Identifiers
An identifier is an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters.
Syntax
The first character of a valid identifier must be one of the following:
- uppercase Latin letters A-Z
- lowercase Latin letters a-z
- underscore
- any Unicode character with the Unicode property XID_Start
Any other character of a valid identifier must be one of the following:
- digits 0-9
- uppercase Latin letters A-Z
- lowercase Latin letters a-z
- underscore
- any Unicode character with the Unicode property XID_Continue
The lists of characters with properties XID_Start and XID_Continue can be found in DerivedCoreProperties.txt.
Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant. Every identifier must conform to Normalization Form C.
Note: Support of Unicode identifiers is limited in most implementations, e.g. gcc (until 10).
In declarations
An identifier can be used to name objects, references, functions, enumerators, types, class members, namespaces, templates, template specializations, parameter packs(since C++11), goto labels, and other entities, with the following exceptions:
- The identifiers that are keywords cannot be used for other purposes.
|
(since C++11) |
- The identifiers that are alternative representations for certain operators and punctuators cannot be used for other purposes.
|
(since C++11) |
- Identifiers that appear as a token or preprocessing token (i.e., not in user-defined-string-literal like
operator ""id)(since C++11) of one of the following forms are reserved:- in the global namespace, identifiers that begin with an underscore
- identifiers that contain a double underscore or begin with an underscore followed by an uppercase letter, except the following identifiers:
- predefined macros (including language feature test macros)(since C++20)
|
(since C++11) |
- the following macros defined in the standard library:
- the C-style I/O library macros
_IOFBF,_IOLBFand_IONBF
- the C-style I/O library macros
|
(since C++11) |
| (since C++20) |
“Reserved” here means that the standard library headers #define or declare such identifiers for their internal needs, the compiler may predefine non-standard identifiers of that kind, and that name mangling algorithm may assume that some of these identifiers are not in use. If the programmer uses such identifiers, the program is ill-formed, no diagnostic required.
In addition, it is undefined behavior to #define or #undef certain names in a translation unit, see reserved macro names for more details.
Zombie identifiers
As of C++14, some identifiers are removed from the C++ standard library. They are listed in the list of zombie names.
However, these identifiers are still reserved for previous standardization in a certain context. Removed member function names may not be used as a name for function-like macros, and other removed member names may not be used as a name for object-like macros in portable code.
In expressions
An identifier that names a variable, a function, specialization of a concept,(since C++20) or an enumerator can be used as an expression. The result of an expression consisting of just the identifier is the entity named by the identifier. The value category of the expression is lvalue if the identifier names a function, a variable, a template parameter object(since C++20), or a data member, and rvalue(until C++11)prvalue(since C++11) otherwise (e.g. an enumerator is an rvalue(until C++11)a prvalue(since C++11) expression, a specialization of a concept is a bool prvalue(since C++20)).
Type
The type of an identifier expression is the same as the type of the entity it names.
|
The following exceptions exist:
void f()
{
float x, &r = x;
[=]
{
decltype(x) y1; // y1 has type float
decltype((x)) y2 = y1; // y2 has type float const& because this lambda
// is not mutable and x is an lvalue
decltype(r) r1 = y1; // r1 has type float&
decltype((r)) r2 = y2; // r2 has type float const&
};
}
|
(since C++11) |
Unqualified identifiers
Besides suitably declared identifiers, the following can be used in expressions in the same role:
- an overloaded operator name in function notation, such as
operator+oroperator new; - a user-defined conversion function name, such as
operator bool;
|
(since C++11) |
- a template name followed by its argument list, such as
MyTemplate<int>; - the character
~followed by a class name, such as~MyClass;
|
(since C++11) |
|