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?
Herb Sutter's books are an excellent source for this topic -- start with http://www.gotw.ca/publications/xc++.htm .
The aphorism for the candidate features is "you don't need it very often, but when you need it, you need it bad."
- Placement new
- extern "C++"
- local classes
- std::allocator
- mutable, explicit, volatile
- pointer-to-any-member-of-any-class
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.
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.
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.
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.
- Destructor should grantee that no exceptions escape.
- Several of the STL algorithms use things like swap which also should grantee no exceptions escape to work correctly in all situations.
- etc...
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.
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.
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.