2

Possible Duplicate:
What are the rules about using an underscore in a C++ identifier?

I'm interested in the relevant sections from the standard (if any).

Community
  • 1
  • 1
Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
  • 4
    For completeness I'll link here the old question http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier and mention that the rules did not change. – R. Martinho Fernandes Oct 16 '12 at 22:21
  • Oh I didn't see that one. Although it's relevant to ask specifically for C++11, I'm not sure what would be the appropriate course of action now. Should this one be closed and the top answer of the other edited? – Tamás Szelei Oct 16 '12 at 22:32

2 Answers2

8

Yes, when the underscore is followed by another underscore or an uppercase letter (i.e. for preprocessor #defines or macros), or if the identifier is in the global namespace (§17.6.4.3.2):

Certain sets of names and function signatures are always reserved to the implementation:

— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.

— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

Note that the first point means that if two underscores appear anywhere in the identifier, even in the middle or at the end, the name is reserved. Also, I would add (§17.6.4.3.5, emphasis mine):

Literal suffix identifiers that do not start with an underscore are reserved for future standardization.

Community
  • 1
  • 1
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • What are literal suffix identifiers exactly? Does this mean that it's OK to use `_name` for private members? – Tamás Szelei Oct 16 '12 at 22:20
  • @fish that rule pertains to user-defined literals, meaning that `foo operator"" _bar(const char*);` is ok, but `foo operator"" bar(const char*);` is reserved – R. Martinho Fernandes Oct 16 '12 at 22:22
  • @fish: It's talking about [user-defined literals.](http://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals) – Nicol Bolas Oct 16 '12 at 22:42
  • I find it important to emphasize that the double underscore rule isn't limited to just the beginning; a double underscore *anywhere* means it is reserved. – chris Oct 16 '12 at 23:23
  • @chris yes, I kinda got that in by saying _for preprocessor #defines_, because they disregard scope. – Seth Carnegie Oct 17 '12 at 02:13
  • 1
    @SethCarnegie, Well, it just took me a while before I realized that `foo__bar` was reserved, not just `__foobar`, and I've seen the same thing from quite a few others. – chris Oct 17 '12 at 02:36
  • @chris oh, I thought you were talking about in any scope. I didn't realise you were talking about "anywhere in the identifer". That is an important point, I'll add a note about that. – Seth Carnegie Oct 17 '12 at 02:56
0

They are not reserved like for but you should not use them, since compiler developers may use them to name their own functions, so it may conflict with your function. So if you use my_class::_function then there should be no error, but write a global function like void _function() may generate a duplicate with one compiler!

BigBoss
  • 6,904
  • 2
  • 23
  • 38
  • While, true in the case you provided, macros ignore scope, so having a member function called `foo__bar` or `_FooBar` can still collide with those. – chris Oct 16 '12 at 23:24