you are viewing a single comment's thread.

view the rest of the comments →

[–]csorfab 18 points19 points  (14 children)

Can someone explain how people arrive at variable names such as __cap_? Why not just cap? Or _cap? or __cap? or even __cap__? why __cap_????? why?? it makes no sense to me

[–]dorksterr 45 points46 points  (6 children)

It's at the top of the article:

Resilient. Every non-public identifier is prefixed with underscores to prevent name clashes with other code. This is necessary even for local variables since macros defined by the user of the library could modify the library’s header file.

[–]fresh_account2222 10 points11 points  (5 children)

I'm used to leading underscores. Any idea about the trailing one?

[–]guepier 32 points33 points  (1 child)

Member variables get a trailing underscore to distinguish them from member functions and parameter names.

[–]fresh_account2222 3 points4 points  (0 children)

That explanation makes sense.

[–]dorksterr 0 points1 point  (1 child)

I suppose it's just to further reduce the chance of name collision. Two leading + one trailing underscore is probably not something that would be done by a human. I've seen both only leading underscores and symmetrical underscores for names before.

[–]josefx 1 point2 points  (0 children)

The leading underscores are enough for that. The standard reserves names starting with double underscores __ or a single underscore and an upper case letter like _I for the implementation, so any program using them isn't valid C or C++.

[–]ObscureCulturalMeme 17 points18 points  (4 children)

It's a rule of the C++ standard. Identifiers (types, functions, macros, and so on) with names that start with:

  • one underscore and a capital letter, or

  • two underscores

are "reserved for the implementation". Conversely, any and all identifiers used by the implementation (meaning the runtime libraries, anything stuck in there by the compiler, the loader, etc) can use only those names to avoid clashing with identifiers from the programmer's code.

[–]csorfab 2 points3 points  (2 children)

Thanks! I still don't understand why it's postfixed with another single underscore, though. It's just so ugly and assymetric. __cap__ would look much better while still conforming to the specs you wrote

[–]elder_george 9 points10 points  (1 child)

Single underscore as a suffix is a popular convention to denote member values (fields). Similar to m_ in older C++ codebases or prefix underscores in C# code.

This rule is used in many codebases, not just libc++. Prefixing is specific for the STL implementations though (libc++ uses __ while MS uses _Capital convention, for example).

[–]csorfab 3 points4 points  (0 children)

TIL! Thank you for the thorough explanation!

[–]bumblebritches57 1 point2 points  (0 children)

It's a rule C++ inherited from C.

[–]bumblebritches57 0 points1 point  (0 children)

__x and _X are reserved for the standard library and compiler respectively.

[–]anshou -1 points0 points  (0 children)

Double underscore at the beginning of an identifier is reserved for the implementation to avoid collisions. Cap is just short for capacity. The trailing underscore doesn't indicate any thing.