C++ Identifiers
An identifier is a sequence of characters used to denote one of the following:
Object or variable name
Class, structure, or union name
Enumerated type name
Member of a class, structure, union, or enumeration
Function or class-member function
typedef name
Label name
Macro name
Macro parameter
The following characters are legal as the first character of an identifier, or any subsequent character:
_ a b c d e f g h i j k l m
n o p q r s t u v w x y z
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
The following characters are legal as any character in an identifier except the first:
0 1 2 3 4 5 6 7 8 9
Microsoft Specific
Only the first 2048 characters of Microsoft C++ identifiers are significant. Names for user-defined types are "decorated" by the compiler to preserve type information. The resultant name, including the type information, cannot be longer than 2048 characters. (See Decorated Names for more information.) Factors that can influence the length of a decorated identifier are:
Whether the identifier denotes an object of user-defined type or a type derived from a user-defined type.
Whether the identifier denotes a function or a type derived from a function.
The number of arguments to a function.
The dollar sign is also a valid identifier in Visual C++.
// dollar_sign_identifier.cpp
struct $Y1$ {
void $Test$() {}
};
int main() {
$Y1$ $x$;
$x$.$Test$();
}
END Microsoft Specific
The first character of an identifier must be an alphabetic character, either uppercase or lowercase, or an underscore ( _ ). Because C++ identifiers are case sensitive, fileName
is different from FileName
.
Identifiers cannot be exactly the same spelling and case as keywords. Identifiers that contain keywords are legal. For example, Pint
is a legal identifier, even though it contains int, which is a keyword.
Use of two sequential underscore characters ( __ ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for C++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers.