Does C++11 allow dollar signs in identifiers?

我怕爱的太早我们不能终老 提交于 2019-12-28 03:05:30

问题


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?


回答1:


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;
    ^



回答2:


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.



来源:https://stackoverflow.com/questions/26301737/does-c11-allow-dollar-signs-in-identifiers

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