< cpp‎ | language

An identifier is an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters (disallowed are control characters and characters in the basic source character set). A valid identifier must begin with a non-digit character (Latin letter, underscore, or Unicode non-digit character). Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant.

Note: C++ grammar formally requires Unicode characters to be escaped with \u or \U, but due to translation phase 1, that is exactly how raw unicode characters from the source code are presented to the compiler. Also note that support of this feature may be limited, e.g. gcc


[edit] In declarations

An identifier can be used to name objects, references, functions, enumerators, types, class members, namespaces, templates, template specializations, parameter packs, goto labels, and other entities, with the following exceptions:

  • the identifiers that are keywords cannot be used for other purposes;
  • the identifiers with a double underscore anywhere are reserved;
  • the identifiers that begin with an underscore followed by an uppercase letter are reserved;
  • the identifiers that begin with an underscore are reserved for use at global namespace.

"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 behavior is undefined.

In addition, it's undefined behavior to #define or #undef names identical to keywords. If at least one standard library header is included, it's undefined behavior to #define or #undef identifiers identical to names declared in any standard library header.

[edit] In expressions

An identifier that names a variable, a function, or an enumerator can be used as an expression. The expression consisting of just the identifier returns the entity named by the identifier. The value category of the expression is lvalue if the identifier names a function, a variable, or a data member, and prvalue otherwise (e.g. an enumerator is a prvalue expression).

Within the body of a non-static member function, each identifier that names a non-static member is implicitly transformed to a class member access expression this->member.

[edit] Unqualified identifiers

Besides suitably declared identifiers, the following can be used in expressions in the same role:

Together with identifiers they are known as unqualified id-expressions.

[edit] Qualified identifiers

A qualified id-expression is an unqualified id-expression prepended by a scope resolution operator ::, and optionally, a sequence of enumeration, (since C++11)class or namespace names or decltype expressions (since C++11) separated by scope resolution operators. For example, the expression std::string::npos is an expression that names the static member npos in the class string in namespace std. The expression ::tolower names the function tolower in the global namespace. The expression ::std::cout names the global variable cout in namespace std, which is a top-level namespace. The expression boost::signals2::connection names the type connection declared in namespace signals2, which is declared in namespace boost.

The keyword template may appear in qualified identifiers as necessary to disambiguate dependent template names.

See qualified lookup for the details of the name lookup for qualified identifiers.

[edit] Names

A name is the use of one of the following to refer to an entity or to a label:

  • an identifier;
  • an overloaded operator name in function notation (operator+, operator new);
  • a user-defined conversion function name (operator bool);
  • a user-defined literal operator name (operator "" _km);
  • a template name followed by its argument list (MyTemplate<int>).

Every name that denotes an entity is introduced into the program by a declaration. Every name that denotes a label is introduced into the program either by a goto statement or by a labeled statement. A name used in more than one translation unit may refer to the same or different entities, depending on linkage.

When the compiler encounters an unknown name in a program, it associates it with the declaration that introduced the name by means of name lookup, except for the dependent names in template declarations and definitions (for those names, the compiler determines whether they name a type, a template, or some other entity, which may require explicit disambiguation).

[edit] See also

C documentation for identifier