views:

396

answers:

8

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?

+3  A: 

Herb Sutter's books are an excellent source for this topic -- start with http://www.gotw.ca/publications/xc++.htm .

Alex Martelli
Herb Sutter's booksa re indeed excellent, but I wouldn't classify the topics they cover as "obscure" - they are things that all working C++ programmers should be aware of.
anon
+4  A: 

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.

Thomas L Holaday
+2  A: 

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.

FeatureCreep
What would you do if something actually did throw bad_alloc? There is no possible recovery, and in fact the box the program is running ion will very likely have crashed long before a bad_alloc could possibly have been thrown.
anon
It depends on the context. What was the reason for the failed allocation? Size? Where did the size come from? Some systems can use a fallback strategy or need some graceful degradation strategy, or just free up some memory. Some systems need to exit nicely with informative logging. In no case is it ok to check for a null pointer without using std::nothrow, which has been the case several times. Also, why do you think the box would necessarily have crashed?
FeatureCreep
Actually, after reading this --> http://www.gotw.ca/publications/mill16.htm, especially the part about the sinister lazy allocation in linux, I can see where your argument comes from. And it applies in a few of the cases I have seen, though not all, since I have seen this in embedded systems where data transfers failed because of the size being bad, and the nullpointer checking is always wrong. But I see your point, I think.
FeatureCreep
@Niel - bad alloc is not program-destroying. It just means the program must free some space (presumably cached data) before retrying.
Tom
A: 

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.

Dario
A: 

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...
Martin York
On the contrary, quite a few books talk about exception safety and guarantees, not least (but not limitted to) those by Herb Sutter.
anon
A: 

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.

Martin York
A: 

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.

Matej
+6  A: 

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.

Dan Olson
+1 I wasn't aware of the ADL Mechanism.
TokenMacGuy