Are dollar-signs allowed in identifiers in C++03?

前端 未结 7 2345
深忆病人
深忆病人 2020-12-03 14:38

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

相关标签:
7条回答
  • 2020-12-03 14:39

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

    0 讨论(0)
  • 2020-12-03 14:42

    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.

    0 讨论(0)
  • 2020-12-03 14:50

    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.

    0 讨论(0)
  • 2020-12-03 14:55

    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).

    0 讨论(0)
  • 2020-12-03 14:59

    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.

    0 讨论(0)
  • 2020-12-03 15:01

    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).

    0 讨论(0)
提交回复
热议问题