views:

844

answers:

6

What does the C++ standard say about using dollar signs in identifiers, such as Hello$World? Are they legal?

+13  A: 

They are illegal. The only legal characters in identifiers are letters, numbers, and _. Identifiers also cannot start with numbers.

Nathaniel Flath
FYI: Although illegal by the standard gcc usually allows them, see http://gcc.gnu.org/onlinedocs/gcc/Dollar-Signs.html#Dollar-Signs
lothar
This is to allow backward compatability. It should not be used for any modern code.
Martin York
A: 

They are not legal in C++. However some C/C++ derived languages (such as Java and JavaScript) do allow them.

Zifre
+11  A: 

A c++ identifier can be composed of any of the following: _ (underscore), the digits 0-9, the letters a-z (both upper and lower case) and cannot start with a number.

There are a number of exceptions as C99 allows extensions to the standard (e.g. visual studio).

Kevin Loney
That's silly that it is allow in VS. MS *always* has to make its own extensions to the standard...
Zifre
Every major C++ compiler has a good handful of extensions. MS is not unique in that.
jalf
GCC also allows dollar signs.
Max Lybbert
You're mising Appendix E - (normative) "Universal-character-names for Identifiers" [extended-id]. However, this adds more letters (either accented or from different scripts), not symbols like $.
MSalters
+1  A: 

Illegal. I think the dollar sign and backtick are the only punctuation marks on my keyboard that aren't used in C++ somewhere (the "%" sign is in format strings, which are in C++ by reference to the C standard).

David Thornley
% is also the modulus operator.
RBerteig
Ah, thank you. That had escaped my mind.
David Thornley
Don't forget the @ character
anon
+4  A: 

Not legal, but many if not most of compilers support them, note this may depend on platform, thus gcc on arm does not support them due to assembly restrictions.

Artyom
+1  A: 

The relevant section is "2.8 Identifiers [lex.name]". From the basic character set, the only valid characters are A-Z a-z 0-9 and _. However, characters like é (U+00E9) are also allowed. Depending on your compiler, you might need to enter é as \u00e9, though.

MSalters