C++ 11标准参考使用类型说明符中允许的类型定义?

And*_*zos 10 c++ language-lawyer specifier type-declaration c++11

在C++ 11中,类型说明符包括类说明符枚举说明符.(又名类定义和枚举定义)

根据语法/语法 - 类型说明符可以出现在语言的几个地方,但不是所有那些地方都允许使用类说明符和枚举说明符.

例如:

struct C{} c;
// ok: types may be defined in the specifiers of a simple declaration

void f(struct S{});
// error: types may not be defined in parameter types

constexpr auto i = sizeof(enum E{});
// error: types may not be defined in ‘sizeof’ expressions
Run Code Online (Sandbox Code Playgroud)

标准中的哪些部分将类型说明符的这些用法划分为可以定义类型和不定义类型的那些?例如,在sizeof表达式中可能没有定义类型的规则在哪里?

Mar*_*k B 8

它在C++标准中找不到的原因是因为它实际上禁止在C标准的增量中使用.

在C.1.4中我们有以下内容:Change: Types must be declared in declarations, not in expressions In C, a sizeof expression or cast expression may create a new type.其中显示了有问题的禁令.

这在7.1.6/3中明确提到:

除非声明构造函数,析构函数或转换函数,否则声明中至少需要一个不是cv-qualifier的类型说明符.92 type-specifier-seq不应定义类或枚举,除非它出现在类型中 - 别名声明(7.1.3)的id,它不是模板声明的声明.

特别感兴趣的部分是那个 A type-specifier-seq shall not define a class or enumeration unless...

  • 有趣的是,这似乎允许*for-range-declaration*:`for(struct S2 {} s2:s){}`中的类型定义,它被GCC接受`-std = c ++ 11 -pedantic`没有任何诊断,但clang给出了明确的"错误:类型可能没有在范围声明中定义" (2认同)