-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 ..

yapkm01
  • 3,590
  • 7
  • 37
  • 62

2 Answers2

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • I think technically is UB (although nothing really happens unless a clash, same as overloading in `std::`) – vsoftco May 18 '15 at 22:42
  • @vsoftco UB ubuntu? Yes. I'm using Ubuntu .. – yapkm01 May 18 '15 at 22:45
  • @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). – Mike Seymour May 18 '15 at 22:49
  • @yapkm01 sorry, I meant undefined behaviour (UB) – vsoftco May 18 '15 at 22:49
  • @MikeSeymour I'm newbie to C++. There's 1 thing i like Java over C++ .. it's predictable. In Java, if we violate the rules (e.g. identifier rules) .. you won't be able to compile. On the other hand, C++ says "Do not to use it but you can if you want. Just don't use those i already use in the standard library". How would i know what those are in the standard library .. ? Freedom is good but could cause grey areas .. – yapkm01 May 18 '15 at 23:05
  • @vsoftco I don't like being voted down without first - further clarification from me. Disrespectful .. – yapkm01 May 18 '15 at 23:08
  • 1
    @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. – Mike Seymour May 18 '15 at 23:09
  • @yapkm01 I did not downvote you, I just marked your question as a duplicate. Personally I thought it was a fair question, but marked it as duplicate since it already has an answer. – vsoftco May 18 '15 at 23:13