C ++ 11允许标识符中的美元符号吗? [英] Does C++11 allow dollar signs in identifiers?
问题描述
C ++ 03中的标识符中是否允许使用美元符号? 涵盖了C ++ 03中不允许使用标识符中的美元符号。 GCC将其作为 C扩展提供,并在C ++中正确提供诊断03模式。但是,在C ++ 11中, int $ = 0
将在没有警告的情况下编译。
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.
http://stackoverflow.com/a/16055409/3920237\">answer 原因可能允许使用 $
,因为实施定义的标识符不需要诊断:
This answer reasons that $
may be allowed because no diagnostic is required for implementation defined identifiers:
这里的答案是可能:根据§2.11
可以包含数字和 identifier-nondigits ,从后者的一个
开始。 identifier-nondigits 是通常的az
,AZ
和
下划线除了自C ++ 11它们包括
通用字符名(例如\\\뺯
,\\UC0FFEE32
),和其他实现定义的字符。因此,如果在标识符中使用$
,那么它是实现定义的
。 VC10和up支持,
也许是较早的版本,太。它甚至支持像
こんばんわ
的标识符。
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.
语言也存在于C ++ 03标准中,所以我不认为这是一个非常有说服力的论据。
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
此外,一些标识符保留供C ++
实现和标准库(17.6.4.3.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?
推荐答案
这是实现定义的行为, $
不包括在标识符的语法中。 C ++ 11 中标识符名称的规则是:
This is implementation defined behavior, $
is not included in grammar for identifiers. The rules for identifier names in C++11 are:
- 不能以数字开头
-
- 可以由字母,数字,下划线,通用字符名称和实现定义的字符组成
- 不能是关键字
- It can not start with a number
- Can be composed of letters, numbers, underscore, universal character names and implementation defined characters
- Can not be a keyword
允许使用实现定义的字符,许多编译器支持作为扩展名,包括 gcc , clang
, Visual Studio ,并在 comment 显然是 DEC C ++编译器。
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.
语法包含在草稿C ++标准部分 2.11
标签,I添加了以< -
开头的附加注释:
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
[...]
如果我们使用 clang
code> -pedantic-errors 标志它不会编译:
If we compile this code using clang
with the -pedantic-errors
flag it will not compile:
int $ = 0
并产生以下错误:
error: '$' in identifier [-Werror,-Wdollar-in-identifier-extension]
int $ = 0;
^
这篇关于C ++ 11允许标识符中的美元符号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!