c++ - typedef中synonym的含义

标签 c++ c++11 standards

以下段落摘自[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 is long int. It is not a synonym for the keyword long. Otherwise you could also say i1 double and get a long double.

也许我应该说,i1 不是type-specifier long 的同义词,但它是类型 long int

关于c++ - typedef中synonym的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31269660/

相关文章:

c++ - exclude/usr/include/c++/4.3/in用intel编译器编译代码

internationalization - 次要货币单位有 ISO 标准吗?

c - c 标准是否保证有符号和无符号的位模式解释?

c++ - 我如何使用 6 位来存储值?

c++ - 配置 QWidget 以通过布局填充父级

c++ - 使用 std::chrono 的 NTP 时间戳

java - C++ 和 Java 中的 "generic"类型有什么区别?

c++ - 属性说明符序列是否继承?

c++ - 在 C++11 中对数组中所有元素的 OR/AND 成员进行 OR/AND 的优雅方式?

c++ - 为什么 RVO 要求如此严格?