Namespaces
Variants

Identifiers

From cppreference.com
Revision as of 04:10, 24 June 2021 by Cubbi (talk | contribs) (p1949r7 became a DR, so down with the bizarre unicode table)
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

An identifier is an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters. A valid identifier must begin with a non-digit character (Latin letter, underscore, or Unicode character of class XID_Start) and may contain non-digit characters, digits, and Unicode characters of class XID_Continue in non-initial positions. Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant.

Note: Support of Unicode identifiers is limited in most implementations, e.g. gcc

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 only place they can be used as non-keywords is in an attribute-token. (e.g. [[private]] is a valid attribute) (since C++11)
  • the identifiers that are alternative representations for certain operators and punctuators cannot be used for other purposes;
  • the identifiers with special meaning (final, import, module(since C++20) and override) are used explicitly in a certain context rather than being regular identifiers;
    • Unless otherwise specified, any ambiguity as to whether a given identifier has a special meaning is resolved to interpret the token as a regular identifier.
(since C++11)
  • 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 in the 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 certain names in a translation unit, see reserved macro names for more details.

Zombie identifiers

Some identifiers only exists in previous standards. In other words, they have been removed. However, they are still reserved for previous standardization in a certain context.

Reserve context Category/Member of Name
Reserved in namespace std Smart pointer auto_ptr
auto_ptr_ref
Function object utility binary_function
binary_negate
bind1st
bind2nd
binder1st
binder2nd
const_mem_fun1_ref_t
const_mem_fun1_t
const_mem_fun_ref_t
const_mem_fun_t
mem_fun1_ref_t
mem_fun1_t
mem_fun_ref_t
mem_fun_ref
mem_fun_t
mem_fun
not1
not2
pointer_to_binary_function
pointer_to_unary_function
ptr_fun
unary_function
unary_negate
Uninitialized storage get_temporary_buffer
raw_storage_iterator
return_temporary_buffer
Error handling get_unexpected
set_unexpected
uncaught_exception
unexpected
unexpected_handler
C-style I/O gets
Type trait is_literal_type
is_literal_type_v
result_of
result_of_t
Algorithm random_shuffle
Reserved as member type
(may not be used as a name for object-like macros in portable code)
std::function argument_type
first_argument_type
second_argument_type
std::ios_base io_state
open_mode
seek_dir
Reserved as member function
(may not be used as a name for function-like macros in portable code)
std::basic_stringbuf stossc

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 prvalue otherwise (e.g. an enumerator is a prvalue expression, a specialization of a concept is a bool prvalue(since C++20)). The type of the expression is determined as follows:

  • If the entity named by the (unqualified) identifier is a local entity, and would result in an intervening lambda expression capturing it by copy if it were named outside of an unevaluated operand in the declarative region in which the identifier appears, then the type of the expression is the type of a class member access expression naming the non-static data member that would be declared for such a capture in the closure object of the innermost such intervening lambda expression.
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)
  • If the entity named is a template parameter object for a template parameter of type T, the type of the expression is const T.
(since C++20)
  • Otherwise, the type of the expression is the same as the type of the entity named.


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.

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.

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.

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).

See also

C documentation for Identifier