Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The rules (which did not change in C++11):</p> <ul> <li>Reserved in any scope, including for use as implementation macros: <ul> <li>identifiers beginning with an underscore followed immediately by an uppercase letter</li> <li>identifiers containing adjacent underscores (or "double underscore")</li> </ul></li> <li>Reserved in the global namespace: <ul> <li>identifiers beginning with an underscore</li> </ul></li> <li>Also, everything in the <code>std</code> namespace is reserved. (You are allowed to add template specializations, though.) </li> </ul> <p>From the 2003 C++ Standard:</p> <blockquote> <h3>17.4.3.1.2 Global names [lib.global.names]</h3> <p>Certain sets of names and function signatures are always reserved to the implementation:</p> <ul> <li>Each name that contains a double underscore (<code>__</code>) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.</li> <li>Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.<sup>165</sup></li> </ul> <p><sup>165)</sup> Such names are also reserved in namespace <code>::std</code> (17.4.3.1). </p> </blockquote> <p>Because C++ is based on the C standard (1.1/2, C++03) and C99 is a normative reference (1.2/1, C++03) these also apply, from the 1999 C Standard:</p> <blockquote> <h3>7.1.3 Reserved identifiers</h3> <p>Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.</p> <ul> <li>All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.</li> <li>All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.</li> <li>Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).</li> <li>All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.<sup>154</sup></li> <li>Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.</li> </ul> <p>No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.</p> <p>If the program removes (with <code>#undef</code>) any macro definition of an identifier in the first group listed above, the behavior is undefined.</p> <p><sup>154)</sup> The list of reserved identifiers with external linkage includes <code>errno</code>, <code>math_errhandling</code>, <code>setjmp</code>, and <code>va_end</code>.</p> </blockquote> <p>Other restrictions might apply. For example, the POSIX standard reserves a lot of identifiers that are likely to show up in normal code:</p> <ul> <li>Names beginning with a capital <code>E</code> followed a digit or uppercase letter: <ul> <li>may be used for additional error code names.</li> </ul></li> <li>Names that begin with either <code>is</code> or <code>to</code> followed by a lowercase letter <ul> <li>may be used for additional character testing and conversion functions.</li> </ul></li> <li>Names that begin with <code>LC_</code> followed by an uppercase letter <ul> <li>may be used for additional macros specifying locale attributes.</li> </ul></li> <li>Names of all existing mathematics functions suffixed with <code>f</code> or <code>l</code> are reserved <ul> <li>for corresponding functions that operate on float and long double arguments, respectively.</li> </ul></li> <li>Names that begin with <code>SIG</code> followed by an uppercase letter are reserved <ul> <li>for additional signal names.</li> </ul></li> <li>Names that begin with <code>SIG_</code> followed by an uppercase letter are reserved <ul> <li>for additional signal actions.</li> </ul></li> <li>Names beginning with <code>str</code>, <code>mem</code>, or <code>wcs</code> followed by a lowercase letter are reserved <ul> <li>for additional string and array functions.</li> </ul></li> <li>Names beginning with <code>PRI</code> or <code>SCN</code> followed by any lowercase letter or <code>X</code> are reserved <ul> <li>for additional format specifier macros</li> </ul></li> <li>Names that end with <code>_t</code> are reserved <ul> <li>for additional type names.</li> </ul></li> </ul> <p>While using these names for your own purposes right now might not cause a problem, they do raise the possibility of conflict with future versions of that standard.</p> <hr> <p>Personally I just don't start identifiers with underscores. New addition to my rule: Don't use double underscores anywhere, which is easy as I rarely use underscore.</p> <p>After doing research on this article I no longer end my identifiers with <code>_t</code> as this is reserved by the POSIX standard.</p> <p>The rule about any identifier ending with <code>_t</code> surprised me a lot. I think that is a POSIX standard (not sure yet) looking for clarification and official chapter and verse. This is from the <a href="http://www.gnu.org/software/libtool/manual/libc/Reserved-Names.html" rel="noreferrer">GNU libtool manual</a>, listing reserved names.</p> <p>CesarB provided the following link to the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_02.html" rel="noreferrer">POSIX 2004</a> reserved symbols and notes 'that many other reserved prefixes and suffixes ... can be found there'. The <a href="http://www.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html" rel="noreferrer">POSIX 2008</a> reserved symbols are defined here. The restrictions are somewhat more nuanced than those above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload