Does C++11 allow dollar signs in identifiers?
This is implementation defined behavior, $
is not included in grammar for identifiers. The rules for identifier names in C++11 are:
- It can not start with a number
- Can be composed of letters, numbers, underscore, universal character names and implementation defined characters
- Can not be a keyword
Implementation-defined characters are allowed and many compilers support as an extension, including gcc, clang
, Visual Studio and as noted in a comment apparently DEC C++ compilers.
The grammar is covered in the draft C++ standard section 2.11
Indentifier, I added additional notes starting with <-
:
identifier:
identifier-nondigit <- Can only start with a non-digit
identifier identifier-nondigit <- Next two rules allows for subsequent
identifier digit <- characters to be those outlined in 2 above
identifier-nondigit:
nondigit <- a-z, A-Z and _
universal-character-name
other implementation-defined characters
[...]
If we compile this code using clang
with the -pedantic-errors
flag it will not compile:
int $ = 0
and generates the following error:
error: '$' in identifier [-Werror,-Wdollar-in-identifier-extension]
int $ = 0;
^
I don't think so. Dollar sign is in ASCII 0x24, which is not inside any of the ranges defined in appendix E.1 (charname.allowed) of the standard. And since it is neither digit nor nondigit it must be an implementation-defined character. I aggree thus that this is not portable C++11. Also note that an identifier shall not start with a universal-character, while it does allow an identifier to start with an character allowed by the implementation.