Reputation:
I've read quite a few beginner's books on C++, and a little beyond that, but what are some of the more obscure aspects of C++, or where can I find information/tutorials on these?
Upvotes: 11
Views: 4302
Reputation: 931
Some obscure C++ features:
Upvotes: 6
Reputation: 5431
No one mentioned what I think it one of the weirdest parts of C++, obviously an after thought, the syntax for the pre-decrement and pre-increment operators.
class A {
public:
A &operator++() {...} // Post increment
A &operator--() {...} // Post decrement
};
vs.
class A {
public:
A &operator++(int) {...} // Pre increment
A &operator--(int) {...} // Pre decrement
};
Of course post increment means add one but return the previous value and pre increment means add one and return the new value. E.g.:
A a;
f(a++); // Post increment a
f(++a); // Pre increment a
I'm not sure what happens if you try to use or pass the 'int' argument. You could try to pass it like this:
obj.operator++(0);
I suppose that is how you differentiate between the two operators when calling them explicitly.
Upvotes: 1
Reputation: 1561
Very obscure:
Just to name a few
Upvotes: 2
Reputation: 23367
ADL (aka Koenig Lookup) is pretty obscure, even though people use it without realizing it in every Hello World program.
The "ScopeGuard trick", where const references as return values from functions are bound to the scope of something they're assigned to, is also fairly obscure. That article raised awareness about it quite a bit, though.
There are also a few properties and usages of sizeof() that count as obscure, especially when used in macros or template metaprograms.
Upvotes: 9
Reputation: 6199
If you want to learn something obscure about C++, try templates in depth and read Modern C++ Design by Andrei Alexandrescu. This book is classics about template metaprogramming.
Upvotes: 0
Reputation: 264381
Not so obvious is the implementation of certain methods in terms of other methods.
Assignment operator:
-> implement as a copy construction and swap.
operator +
-> implemented as copy construction and operator +=
operator !=
-> implement in terms of operator ==
etc...
The concept is that the work should be localized in one method and the other methods use this method to do the real work then add their unique twist to it.
Upvotes: 0
Reputation: 264381
All books explain what exceptions are.
But very few talk about exception safety and the exception grantees.
How to use RAII (easy example smart pointers) to make your code exception safe.
What are the exception guarantees that you should provide.
Upvotes: 1
Reputation: 1824
Actually caring about bad_alloc.
no?
Edit: what I mean is that in many of the sometimes huge C++ projects in which I have had the pleasure to fix bugs, the concept of catching bad_alloc and acting upon it have been missing. That would put it in the "obscure" part of C++, even though it should not be.
Upvotes: 4
Reputation: 49218
Template metaprogramming (an entire litte turing-complete programming language in C++ executed by the compiler) and hacks with the preprocessor can be very hard! (You can even create completely new syntax with this - just take a look at boost::lambda
)
But I think the most important thing to learn and understand is the STL (C++ standard library) which is inevitably useful but may look somewhat strange.
Upvotes: 1
Reputation: 13814
The aphorism for the candidate features is "you don't need it very often, but when you need it, you need it bad."
So, for the people who have had reason to use these features (library authors), they won't be obscure, and for the majority of C++ programmers, they will be unknown.
Upvotes: 6
Reputation: 881595
Herb Sutter's books are an excellent source for this topic -- start with http://www.gotw.ca/publications/xc++.htm .
Upvotes: 2