-1

According to the C++ Primer 5th Ed by Stanley Lipp:

The standard also reserves a set of names for use in the standard library. The identifiers we define in our own programs may not contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an uppercase letter. In addition, identifiers defined outside a function may not begin with an underscore.

All the following did compiled:

g++ -std=c++11 -o test test.cc

int _I=40; int main() { int __=10; int _B=20; } 

I thought it shouldn't compile ..

1
  • You have undefined behaviour, see the answer in the dupe. Commented May 18, 2015 at 22:41

2 Answers 2

4

The text says "The standard also reserves a set of names" - this does not necessarily mean that names of that form will cause a compiler error or warning. It's just that if you choose to use names of that form, they may conflict with other names defined by the compiler or libraries.

Sign up to request clarification or add additional context in comments.

Comments

1

The quote means that there's no guarantee that such names will work: they can conflict with names used by the implementation.

As Mike Seymour write in a comment here,

You don't need to know the names in the standard library. You just need to know which names are reserved - which you described in one short sentence in the question. The compiler can't tell whether it's compiling your code or the library's, so it can't tell whether or not such names should be allowed - you just need to know this rule (which you do, since you asked a question about it) and follow it.

8 Comments

I think technically is UB (although nothing really happens unless a clash, same as overloading in std::)
@vsoftco UB ubuntu? Yes. I'm using Ubuntu ..
@yapkm01: UB == "undefined behaviour" (meaning a program error that the compiler probably won't diagnose for you, and which could make the program fail in unpredictable ways - something you very much want to avoid).
@yapkm01 sorry, I meant undefined behaviour (UB)
@yapkm01: You don't need to know the names in the standard library. You just need to know which names are reserved - which you described in one short sentence in the question. The compiler can't tell whether it's compiling your code or the library's, so it can't tell whether or not such names should be allowed - you just need to know this rule (which you do, since you asked a question about it) and follow it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.