问题
What does the C++ standard say about using dollar signs in identifiers, such as Hello$World
? Are they legal?
回答1:
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).
回答2:
They are illegal. The only legal characters in identifiers are letters, numbers, and _. Identifiers also cannot start with numbers.
回答3:
In C++03, the answers given earlier are correct: they are illegal. In C++11 the situation changed however:
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.
回答4:
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.
回答5:
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.
回答6:
They are not legal in C++. However some C/C++ derived languages (such as Java and JavaScript) do allow them.
回答7:
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).
来源:https://stackoverflow.com/questions/936744/are-dollar-signs-allowed-in-identifiers-in-c03