24

Are dollar-signs allowed in identifiers in C++03? covers that dollar signs in identifiers are not allowed in C++03. GCC provides it as a C extension and properly gives a diagnostic in C++03 mode. However, in C++11, int $ = 0 will compile without warning.

This answer reasons that $ may be allowed because no diagnostic is required for implementation defined identifiers:

The answer here is "Maybe": According to §2.11, identifiers may consist of digits and identifier-nondigits, starting with one of the latter. identifier-nondigits are the usual a-z, A-Z and underscore, in addition since C++11 they include universal-character-names (e.g. \uBEAF, \UC0FFEE32), and other implementation-defined characters. So it is implementation defined if using $ in an identifier is allowed. VC10 and up supports that, maybe earlier versions, too. It even supports identifiers like こんばんわ.

But: I wouldn't use them. Make identifiers as readable and portable as possible. $ is implementation defined and thus not portable.

This language is present in the C++03 standard as well, so I don't find this to be a very convincing argument.

§2.10/2

In addition, some identifiers are reserved for use by C ++ implementations and standard libraries (17.6.4.3.2) and shall not be used otherwise; no diagnostic is required.

What change in the standard allows $ to be used as an identifier name?

Community
  • 1
  • 1
  • 2
    Clang warns on this. – T.C. Oct 10 '14 at 14:39
  • @Shafik Confusingly enough this [answer](http://stackoverflow.com/a/14595459/3920237) links to old gcc documentation that explicitly says that C++ forbids `$` in identifiers. They moved it to [preprocessor options](https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html#Preprocessor-Options) with the only description `Accept ‘$’ in identifiers.` –  Oct 10 '14 at 14:52
  • Historically, DEC used $ in all of its system service and library function names with $$ in all of its internal functions. All the DEC compilers either extensions allowing $ symbol names or pragmas for mapping (as with ADA). – user3344003 Oct 10 '14 at 15:54
  • With GCC 4.9.2, I found that $ seems to be allowed except as the first character. That really surprised me. I first saw this used recently in the hilarious BASIC emulator at https://github.com/rollbear/basicp. I looked at the source to find out how on earth the $ symbol was supported. Surprisingly: natively! But, obviously, $ should be avoided as it does not seem to be standard or portable. (On the other hand, GCC didn't like £. Boo!) – Rhubbarb Jun 25 '15 at 14:19

2 Answers2

22

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;
    ^
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
3

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.

dom0
  • 7,356
  • 3
  • 28
  • 50
  • 1
    "Not portable" and "not allowed" (ill-formed) are both bad things, but not the same. So this isn't a direct answer to the question. – MSalters Oct 10 '14 at 18:28
  • 1
    I am well aware of that. I used "not portable" as a synonym for "implementation-defined behaviour"here. – dom0 Oct 10 '14 at 18:31