views:

523

answers:

2

For instance why does most members in STL implementation have M or _ or __ prefix? Why there is so much boilerplate code ?

What features C++ is lacking that would allow make vector (for instance) implementation clear and more concise?

+17  A: 

Implementatons use names starting with underscore and a capital letter or two underscores to avoid confilcts with user defined macros. Such names are reserved in C++. For example one could define a macro called Type and then include <vector>, if vector implementation used Type as a name of template parameter it would break. However, one is not allowed to define macro called _Type (or __type, type__ etc.), therefore vector can safely use such names.

robson3.14
+1 Indeed. GCC's implementation even contains a comment explaining why queue's underlying container is called c and is not "uglified as per style guidelines".
UncleBens
So C++ could have been improved by making macros system more powerfull :)
Łukasz Lew
namespaces for macros?
Aardvark
That still doesn't protect against poorly named user defined macros that conflict with names of types or member functions (e.g. #define size 2).
bk1e
+2  A: 

Lots of STL implementations also include checking for debug builds, such as verifying that two iterators are from the same container when comparing them, and watching for iterators going out of bounds. This involves fairly complex code to track the container and validity of every iterator created, but is invaluable for finding bugs. This code is also all interwoven with the standard release code with #ifdefs - even in the STL algorithms. So it's never going to be as clear as their most basic operation. Sites like this one show the most basic functionality of STL algorithms, stating their functionality is "equivalent to" the code they show. You won't see that in your header files though.

AshleysBrain