97
votes

I was taking a look through some open-source C++ code and I noticed a lot of double underscores used within 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's code styles? I would think that it makes it hard to read.

7
Why hard to read? It's designed mostly as a delimeter, just like quotes. As I recall, it's mainly used for builtin constants.Matthew Scharley
No, it's not a delimiter. The underscores are used to distinguish names reserved for the implementation from names that users' source code can use. Users can do #define FOO 1 but they must not do #define __FOO__ 1 and therefore the implementation is free to use the name __FOO__ for its own macros, variables, functions etc.Jonathan Wakely
I think Matthew meant it is stylistically/visually a delimeter, not functionally. Which is an interesting hypothesis, but incorrect given what I have previously read and Jonathan's answer.twitchdotcom slash KANJICODER

7 Answers

132
votes

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.

52
votes

Unless they feel that they are "part of the implementation", i.e. 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, i.e. they might be used for macros.

In addition, all other identifiers which start with an underscore (i.e. 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.

38
votes

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

11
votes

The foregoing comments are correct. __Symbol__ is generally a magic token provided by your helpful compiler (or preprocessor) vendor. Perhaps the most widely-used of these are __FILE__ and __LINE__, which are expanded by the C preprocessor to indicate the current filename and line number. That's handy when you want to log some sort of program assertion failure, including the textual location of the error.

8
votes

It's something you're not meant to do in 'normal' code. This ensures that compilers and system libraries can define symbols that won't collide with yours.

5
votes

Double underscores are reserved to the implementation

The top voted answer cites 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."

However, after reading a few C++ and C standards, I was unable to find any mention of underscores being restricted to just the compiler's internal use. The standards are more general, reserving double underscores for the implementation.

C++

C++ (current working draft, accessed 2019-5-26) states in lex.name:

  • Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.
  • Each identifier that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

C

Although this question is specific to C++, I've cited relevant sections from C standards 99 and 17:

C99 section 7.1.3

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

C17 says the same thing as C99.

What is the implementation?

For C/C++, the implementation loosely refers to the set resources necessary to produce an executable from user source files. This includes:

  • preprocessor
  • compiler
  • linker
  • standard library

Example implementations

There are a number of different C++ implementations mentioned on Wikipedia. (no anchor link, ctrl+f "implementation")

Here's an example of Digital Mars' C/C++ implementation reserving some keywords for a feature of theirs.

3
votes

In addition to libraries which many other people responded about, Some people also name macros or #define values for use with the preprocessor. This would make it easier to work with, and may have allowed bugs in older compilers to be worked around.

Like others mentioned, it helps prevent name collision and helps to delineate between library variables and your own.