以下段落摘自[dcl.typedef]:
Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in Clause 8. A typedef-name is thus a synonym for another type. A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.
我们需要的其他相关段落来自[dcl.type]
As a general rule, at most one type-specifier is allowed in the complete decl-specifier-seq of a declaration or in a type-specifier-seq or trailing-type-specifier-seq. The only exceptions to this rule are the following: … long can be combined with long.
在下面的代码中,i1
只是long的同义词。
typedef long i1;
typedef long i1 i2;
因此,我希望第二行可以理解为 typedef long long i2
。然而,MSVC2015RC 失败了
Error C2146 syntax error: missing ';' before identifier 'i2'
谁能指出标准中使我的期望无效的部分?
更新
我的观点是,据我对 [dcl.type] 中语法的理解,
type-specifier:
trailing-type-specifier
class-specifier
enum-specifier
trailing-type-specifier:
simple-type-specifier
elaborated-type-specifier
typename-specifier
cv-qualifier
type-specifier-seq:
type-specifier attribute-specifier-seq opt
type-specifier type-specifier-seq
trailing-type-specifier-seq:
trailing-type-specifier attribute-specifier-seq opt
trailing-type-specifier trailing-type-specifier-seq
decl-specifier-seq 确实允许一系列类型说明符,只要它们满足组合规则。在我看来,类型与类型说明符不同,即使类型是由类型说明符指定的;-)
最佳答案
好的,我来回答。
首先,看看这个:
a typedef-name is syntactically equivalent to a keyword
这仅意味着 typedef-names 遵循关键字的语法。这并不意味着 typedef-name 等同于任何特定的关键字。它就像一个新的、独特的关键字。
然后我们有,
A typedef-name is thus a synonym for another type.
那么,给定 typedef long i1;
,这个“另一种类型”是什么?它是 long int
,而不仅仅是 long
。
另外,什么是“类型”?至少,类型说明符不是类型。类型说明符 long
表示类型“long int”(参见 n3337 的表 10 或 n4296 的表 9)。
我会在这里复制我的评论:
i1
is a synonym for the type that islong int
. It is not a synonym for the keywordlong
. Otherwise you could also sayi1 double
and get along double
.
也许我应该说,i1
不是type-specifier long
的同义词,但它是类型 long int
。
关于c++ - typedef中synonym的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31269660/