Why is the STL implementation so unreadable? How could C ++ be improved here?

For example, why do most members in the STL implementation have the prefix _M_ or _ or __ ? Why so many patterns?

What features of C ++ are missing that will make the implementation of the vector (for example) understandable and more concise?

+28
c ++ c ++ 11 stl readability
Sep 22 '09 at 15:51
source share
3 answers

Implementations use names beginning with an underscore followed by a capital letter or two underscores to avoid conflicts with user-defined macros. Such names are reserved in C ++. For example, you can define a macro called Type , and then #include <vector> . If the vector implementation used Type as the name of the template parameter, it would break. However, macros called _Type (or __type , type__ , etc.) cannot be defined. Therefore, vector can safely use such names.

+34
Sep 22 '09 at 16:04
source share

Many STL implementations also include checking debug assemblies, such as checking that two iterators are from the same container when comparing them, and viewing iterators that go beyond. This includes fairly sophisticated code to track the container and the credibility of each iterator created, but it is invaluable for finding bugs. This code is also interwoven with the standard C # release code ifdefs - even in STL algorithms. Therefore, it will never be as clear as their most basic operation. Sites such as this show the most basic functionality of STL algorithms, stating that their functionality is "equivalent" to the code that they show. However, you will not see this in your header files.

+5
Sep 22 '09 at 17:03
source share

In addition to the good reasons that robson and AshleysBrain have already given, one of the reasons that standard C ++ libraries have such short names and compact code is that almost every C ++ program (a compilation unit, really ) includes a large number of the standard library headers, and they are thus recompiled (remember that they are largely template-based, while the headers of the standard C library contain only a few function declarations). The standard library, written in the style of the "standard standard", requires more time to compile and, thus, leads to the perception that a particular compiler is "slow". By minimizing spaces and using short identifier names, lexer and parser have less work, and the whole compilation process completes a little faster.

Another reason worth mentioning is that many standard library implementations (e.g. Dinkumware, Rogue Wave (old), etc.) can be used with several different compilers with very different compliance standards and quirks. Often there are many macros designed to satisfy each supported platform.

+2
Nov 15 '10 at 2:27
source share



All Articles