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;
^
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…