(double underscore) so much in C++

I was having a look through some open source C++ code and notice a lot of double under scores where used in the code, mainly at the start of variable names.

return __CYGWIN__;

Just wondering is there a reason for this, or is it just some people code styles? I would think that I makes it hard to read.


From Programming in C++, Rules and Recommendations :

The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.

Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.


Unless they feel that they are "part of the implementation", ie the standard libraries, then they shouldn't.

The rules are fairly specific, and are slightly more detailed than some others have suggested.

All identifiers that contain a double underscore or start with an underscore followed by an uppercase letter are reserved for the use of the implementation at all scopes, ie they might be used for macros.

In addition, all other identifiers which start with an underscore (ie not followed by another underscore or an uppercase letter) are reserved for the implementation at the global scope. This means that you can use these identifiers in your own namespaces or in class definitions.

This is why Microsoft use function names with a leading underscore and all in lowercase for many of their core runtime library functions which aren't part of the C++ standard. These function names are guaranteed not to clash with either standard C++ functions or user code functions.


According to the C++ Standard, identifiers starting with one underscore are reserved for libraries. Identifiers starting with two underscores are reserved for compiler vendors.

链接地址: http://www.djcxy.com/p/6880.html

上一篇: 什么是移动语义?

下一篇: (双下划线)在C ++中如此之多