0

#ifndef is used to tell the compiler that a given file should only be included once, as specified here: Why are #ifndef and #define used in C++ header files?.

But often times, I see:

#ifndef __somecode__

rather than:

#ifndef somecode

Is there a good reason to do this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
wsb memes
  • 69
  • 6
  • 4
    Presumably it is to reduce the risk of the `#define` conflicting with other identifiers. But you aren't supposed to use identifiers that begin with or contain two consecutive underscores as they are reserved for the implementation. The language requires that you don't use these kinds of names. – François Andrieux Dec 23 '21 at 16:08
  • 5
    Symbols that begin with double underscore are reserved for the implementation, so using this in any code (library or application) is poor practice unless you can guarantee that you will never collide with any symbols use by any of your current target compilers and environments, and you can guarantee that someone will maintain this guarantee going forward. Just don't do it. – JohnFilleau Dec 23 '21 at 16:08
  • "#ifndef is used to tell the compiler..." No! The compiler does not see the `include` directives. That is the task of the [preprocessor](https://en.cppreference.com/w/cpp/preprocessor). Those are preprocessor directives. – digito_evo Dec 23 '21 at 16:08
  • If you see this in the implementation's libraries (c++std lib code that ships with your compiler) then that's valid. Don't copy that code though, it's unreadable. – JohnFilleau Dec 23 '21 at 16:09
  • Addendum: you may be tempted to just use symbols that start with three underscores, since those aren't reserved, right? Don't do that either. Something that starts with three underscores must necessarily start with two underscores as well. The implementation reserves all prefixes of 2 + N underscores for all N >= 0. – JohnFilleau Dec 23 '21 at 16:11
  • https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL51-CPP.+Do+not+declare+or+define+a+reserved+identifier – JohnFilleau Dec 23 '21 at 16:12
  • 1
    @digito_evo: The preprocessor is commonly part of the compiler these days, and it is part of the language standard. The distinction of preprocessing as a separate process is archaic. – Eric Postpischil Dec 23 '21 at 16:13
  • Does this answer your question? [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – phuclv Dec 23 '21 at 16:14
  • Names that **contain** two consecutive underscores and names that begin with an underscore followed by a capital letter are reserved for use by the implementation. – Pete Becker Dec 23 '21 at 16:51

1 Answers1

4

(prefix) __ for inclusion guards

There a good reason to do this, when the code is part of the implementation's library.

Using a name containing a __ is reserved for the implementation. It will not conflict with good user code.

Your user code should not do this.

17.6.4.3.2 Global names [global.names]
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 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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256