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

孤人 提交于 2019-11-27 14:40:45

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

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

Arne Mertz

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.

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.

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.

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!