4

C++ Primer says:

The identifier we define in our programs may not contain 2 consecutive underscores, nor can identifier begin with an underscore followed immediately by an uppercase letter. In addition, identifiers be fined outside of a function may not begin with an underscore

All is well, but

int _c = 55; // outside function starts with _ int main () { int _A = 12; // _ followed by uppercase letter cout << _A << endl; int __b__ =33; // 2 consecutive __ cout << __b__ << endl; cout << _c << endl; } 

Code above compiles perfectly fine on mac, g++ 4.7.1, using the following flags

g++ -pedantic -Wall -Werror -std=c++11 -O3 -funroll-loops -fprefetch-loop-arrays 

What am i missing please?

4
  • 3
    When you break the "rules", it's doesn't guarantee that it will not work. It just may or may not. Commented Aug 18, 2012 at 19:13
  • 1
    Related: stackoverflow.com/questions/228783/… Commented Aug 18, 2012 at 19:13
  • NOt everything that isn't correct must fail to compile. Much like not every sentence that you can form in the English language must automatically make sense. Commented Aug 18, 2012 at 20:16
  • Also, you should avoid using identifiers that begins with an underscore and followed by lowercase letter (not only uppercase letters as mentioned in the book). In my compiler and OS, _pthread_cleanup_buffer is already used. Commented May 4, 2022 at 13:01

2 Answers 2

9

Crossing the street without looking out for traffic both ways doesn't guarantee that you are run over by a bus, but it is still a bad idea.

One of those days it isn't going to work...

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

Comments

4

Nothing. The identifiers with a leading underscore followed by an uppercase letter and identifiers with two underscores are merely reserved.

Your compiler vendor might decide to use them at any time (for instance, it's perfectly fine for a compiler vendor to use _A as a keyword) and hence you should not use them. Often, the standard library is implemented using reserved identifiers, hence they do not result in a compile error.

2 Comments

Is there a way to treat these as warnings?
Nope, that wouldn't work, as the compiler does not know what code is from the standard library and which one is yours.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.