Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/identifiers"

From cppreference.com
< cpp‎ | language
(Added some reserved names (not completed yet).)
(Added zombie identifiers)
Line 8: Line 8:
 
An identifier can be used {{rlp|declarations|to name}} objects, references, functions, enumerators, types, class members, namespaces, templates, template specializations, parameter packs, goto labels, and other entities, with the following exceptions:
 
An identifier can be used {{rlp|declarations|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 [[cpp/keyword|keywords]] cannot be used for other purposes;
 
* the identifiers that are [[cpp/keyword|keywords]] cannot be used for other purposes;
 +
{{rev begin}}
 
** The only place they can be used as non-keywords is in an {{spar|attribute-token}}. (e.g. {{c|[[private]]}} is a valid {{rlp|attributes|attribute}})
 
** The only place they can be used as non-keywords is in an {{spar|attribute-token}}. (e.g. {{c|[[private]]}} is a valid {{rlp|attributes|attribute}})
 
* the identifiers that are {{rlp|operator_alternative|alternative representations}} for certain operators and punctuators cannot be used for other purposes;
 
* the identifiers that are {{rlp|operator_alternative|alternative representations}} for certain operators and punctuators cannot be used for other purposes;
Line 24: Line 25:
 
In addition, it's undefined behavior to {{tt|#define}} or {{tt|#undef}} certain names in a translation unit, see [[cpp/preprocessor/replace|reserved macro names]] for more details.
 
In addition, it's undefined behavior to {{tt|#define}} or {{tt|#undef}} certain names in a translation unit, see [[cpp/preprocessor/replace|reserved macro names]] for more details.
  
{{todo|+other contents of N4860 16.5.4.3 [reserved.names]}}
+
====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.
 +
 
 +
{| class="wikitable" style="text-align:center;"
 +
|-
 +
! Reserve context
 +
! Category/Member of
 +
! Name
 +
|-
 +
| rowspan="39" | Reserved in namespace {{c|std}}
 +
| rowspan="2" | [[cpp/memory|Smart pointer]]
 +
|[[cpp/memory/auto_ptr|auto_ptr]]
 +
|-
 +
|[[cpp/memory/auto_ptr|auto_ptr_ref]]
 +
|-
 +
| rowspan="23" | [[cpp/utility/functional|Function object utility]]
 +
|[[cpp/utility/functional/binary_function|binary_function]]
 +
|-
 +
|[[cpp/utility/functional/binary_negate|binary_negate]]
 +
|-
 +
|[[cpp/utility/functional/bind12|bind1st]]
 +
|-
 +
|[[cpp/utility/functional/bind12|bind2nd]]
 +
|-
 +
|[[cpp/utility/functional/binder12|binder1st]]
 +
|-
 +
|[[cpp/utility/functional/binder12|binder2nd]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_ref_t|const_mem_fun1_ref_t]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_t|const_mem_fun1_t]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_ref_t|const_mem_fun_ref_t]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_t|const_mem_fun_t]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_ref_t|mem_fun1_ref_t]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_t|mem_fun1_t]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_ref_t|mem_fun_ref_t]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_ref|mem_fun_ref]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun_t|mem_fun_t]]
 +
|-
 +
|[[cpp/utility/functional/mem_fun|mem_fun]]
 +
|-
 +
|[[cpp/utility/functional/not1|not1]]
 +
|-
 +
|[[cpp/utility/functional/not2|not2]]
 +
|-
 +
|[[cpp/utility/functional/pointer_to_binary_function|pointer_to_binary_function]]
 +
|-
 +
|[[cpp/utility/functional/pointer_to_unary_function|pointer_to_unary_function]]
 +
|-
 +
|[[cpp/utility/functional/ptr_fun|ptr_fun]]
 +
|-
 +
|[[cpp/utility/functional/unary_function|unary_function]]
 +
|-
 +
|[[cpp/utility/functional/unary_negate|unary_negate]]
 +
|-
 +
| rowspan="3" | [[cpp/memory|Uninitialized storage]]
 +
|[[cpp/memory/get_temporary_buffer|get_temporary_buffer]]
 +
|-
 +
|[[cpp/memory/raw_storage_iterator|raw_storage_iterator]]
 +
|-
 +
|[[cpp/memory/return_temporary_buffer|return_temporary_buffer]]
 +
|-
 +
| rowspan="5" | [[cpp/error|Error handling]]
 +
|[[cpp/error/get_unexpected|get_unexpected]]
 +
|-
 +
|[[cpp/error/set_unexpected|set_unexpected]]
 +
|-
 +
|[[cpp/error/uncaught_exception|uncaught_exception]]
 +
|-
 +
|[[cpp/error/unexpected|unexpected]]
 +
|-
 +
|[[cpp/error/unexpected_handler|unexpected_handler]]
 +
|-
 +
|[[cpp/io/c|C-style I/O]]
 +
|[[cpp/io/c/gets|gets]]
 +
|-
 +
| rowspan="4" | [[cpp/header/type_traits|Type trait]]
 +
|[[cpp/types/is_literal_type|is_literal_type]]
 +
|-
 +
|[[cpp/types/is_literal_type|is_literal_type_v]]
 +
|-
 +
|[[cpp/types/result_of|result_of]]
 +
|-
 +
|[[cpp/types/result_of|result_of_t]]
 +
|-
 +
|[[cpp/algorithm|Algorithm]]
 +
|[[cpp/algorithm/random_shuffle|random_shuffle]]
 +
|-
 +
| rowspan="6" | Reserved as member type<br>(may not be used as a name for object-like macros in portable code)
 +
| rowspan="3" | [[cpp/utility/functional/function|std::function]]
 +
|argument_type
 +
|-
 +
|first_argument_type
 +
|-
 +
|second_argument_type
 +
|-
 +
| rowspan="3" | [[cpp/io/ios_base|std::ios_base]]
 +
|io_state
 +
|-
 +
|open_mode
 +
|-
 +
|seek_dir
 +
|-
 +
|Reserved as member function<br>(may not be used as a name for function-like macros in portable code)
 +
|[[cpp/io/basic_stringbuf|std::basic_stringbuf]]
 +
|stossc
 +
|-
 +
|}
  
 
===In expressions===
 
===In expressions===

Revision as of 01:25, 7 July 2020

 
 
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 (see below for details). 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

Contents

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

Unicode characters in identifiers

The following Unicode character ranges are allowed in identifiers:

Code points Description Characters
U+00A8 DIARESIS ¨
U+00AA FEMININE ORDINAL INDICATOR ª
U+00AD SOFT HYPHEN ­
U+00AF MACRON ¯
U+00B2 - U+00B5 SUPERSCRIPT TWO - MICRO SIGN ²³´µ
U+00B7 - U+00BA MIDDLE DOT - MASCULINE ORDINAL INDICATOR ·¸¹º
U+00BC - U+00BE VULGAR FRACTION ONE QUARTER - VULGAR FRACTION THREE QUARTERS ¼½¾
U+00C0 - U+00D6 LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS ÀÁÂ...ÔÕÖ
U+00D8 - U+00F6 LATIN CAPITAL LETTER O WITH STROKE - LATIN SMALL LETTER O WITH DIAERESIS ØÙÚ...ôõö
U+00F8 - U+167F LATIN SMALL LETTER O WITH STROKE - CANADIAN SYLLABICS BLACKFOOT W øùú...ᙽᙾᙿ
U+1681 - U+180D OGHAM LETTER BEITH - MONGOLIAN FREE VARIATION SELECTOR THREE ᚁᚂᚃ...᠋᠌᠍
U+180F - U+1FFF SYRIAC LETTER BETH - GREEK DASIA ᠏ܒܓ...´῾🿾
U+200B - U+200D ZERO WIDTH SPACE - ZERO WIDTH JOINER ​‌‍
U+202A - U+202E LEFT-TO-RIGHT EMBEDDING - RIGHT-TO-LEFT OVERRIDE
U+203F - U+2040 UNDERTIE - CHARACTER TIE ‿⁀
U+2054 INVERTED UNDERTIE
U+2060 - U+218F WORD JOINER - TURNED DIGIT THREE ...↉↊↋
U+2460 - U+24FF CIRCLED DIGIT ONE - NEGATIVE CIRCLED DIGIT ZERO ①②③...⓽⓾⓿
U+2776 - U+2793 DINGBAT NEGATIVE CIRCLED DIGIT ONE - DINGBAT NEGATIVE CIRCLED SANS-SERIF NUMBER TEN ❶❷❸...➑➒➓
U+2C00 - U+2DFF GLAGOLITIC CAPITAL LETTER AZU - COMBINING CYRILLIC LETTER IOTIFIED BIG YUS ⰀⰁⰂ...
U+2E80 - U+2FFF CJK RADICAL REPEAT - IDEOGRAPHIC DESCRIPTION CHARACTER OVERLAID ⺀⺁⺂...⿹⿺⿻
U+3004 - U+3007 JAPANESE INDUSTRIAL STANDARD SYMBOL - IDEOGRAPHIC NUMBER ZERO 〄々〆〇
U+3021 - U+302F HANGZHOU NUMERAL ONE - HANGUL DOUBLE DOT TONE MARK 〡〢〣...
U+3031 - U+D7FF VERTICAL KANA REPEAT MARK - HANGUL JONGSEONG PHIEUPH-THIEUTH ...
U+F900 - U+FD3D CJK COMPATIBILITY IDEOGRAPH-F900 - ARABIC LIGATURE ALEF WITH FATHATAN ISOLATED FORM 豈更車...ﴻﴼﴽ
U+FD40 - U+FDCF ARABIC LIGATURE TEH WITH JEEM WITH MEEM INITIAL FORM -
ARABIC LIGATURE NOON WITH JEEM WITH YEH FINAL FORM
U+FDF0 - U+FE44 ARABIC LIGATURE SALLA USED AS KORANIC STOP SIGN ISOLATED FORM -
PRESENTATION FORM FOR VERTICAL RIGHT WHITE CORNER BRACKET
...﹂﹃﹄
U+FE47 - U+FFFD PRESENTATION FORM FOR VERTICAL LEFT SQUARE BRACKET - REPLACEMENT CHARACTER ﹇﹈﹉...�
U+10000 - U+1FFFD LINEAR B SYLLABLE B008 A - CHEESE WEDGE (U+1F9C0)
U+20000 - U+2FFFD <CJK Ideograph Extension B, First> - CJK COMPATIBILITY IDEOGRAPH-2FA1D (U+2FA1D)
U+30000 - U+3FFFD
U+40000 - U+4FFFD
U+50000 - U+5FFFD
U+60000 - U+6FFFD
U+70000 - U+7FFFD
U+80000 - U+8FFFD
U+90000 - U+9FFFD
U+A0000 - U+AFFFD
U+B0000 - U+BFFFD
U+C0000 - U+CFFFD
U+D0000 - U+DFFFD
U+E0000 - U+EFFFD LANGUAGE TAG (U+E0001) - VARIATION SELECTOR-256 (U+E01EF)


The following Unicode character ranges are not allowed to begin an identifier:

Code points Description Characters
U+0300 - U+036F COMBINING GRAVE ACCENT - COMBINING LATIN SMALL LETTER X
U+1DC0 - U+1DFF COMBINING DOTTED GRAVE ACCENT - COMBINING RIGHT ARROWHEAD AND DOWN ARROWHEAD BELOW
U+20D0 - U+20FF COMBINING LEFT HARPOON ABOVE - COMBINING ASTERISK ABOVE
U+FE20 - U+FE2F COMBINING LIGATURE LEFT HALF - COMBINING CYRILLIC TITLO RIGHT HALF

See also

C documentation for identifier