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:

  1. It can not start with a number
  2. Can be composed of letters, numbers, underscore, universal character names and implementation defined characters
  3. 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;
    ^

Leave a Comment