简体   繁体   English

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

[英]C++11 standard ref for allowed type definitions in type specifier uses?

In C++11 a type specifier includes class specifiers and enum specifiers . 在C ++ 11中, 类型说明符包括类说明符枚举说明符 (aka class definitions and enumeration definitions) (又名类定义和枚举定义)

According to the grammar/syntax - type specifiers can appear in several places in the language, but not in all those places are class specifiers and enum specifiers allowed. 根据语法/语法 - 类型说明符可以出现在语言的几个地方,但不是所有那些地方都允许使用类说明符和枚举说明符。

For example: 例如:

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

Where in the standard does it partition these uses of type specifiers into those where types may and may not be defined? 标准中的哪些部分将类型说明符的这些用法划分为可以定义类型和不定义类型的那些? For example, where is the rule that says types may not be defined in a sizeof expression? 例如,在sizeof表达式中可能没有定义类型的规则在哪里?

The reason it can't be found in the C++ standard is because it's actually prohibited in a delta from the C standard. 它在C ++标准中找不到的原因是因为它实际上禁止在C标准的增量中使用。

In C.1.4 we have the following: Change: Types must be declared in declarations, not in expressions In C, a sizeof expression or cast expression may create a new type. 在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. which shows the prohibition in question. 这显示了有问题的禁令。

This is explicitly called out in 7.1.6/3: 这在7.1.6 / 3中明确提到:

At least one type-specifier that is not a cv-qualifier is required in a declaration unless it declares a constructor, destructor or conversion function.92 A type-specifier-seq shall not define a class or enumeration unless it appears in the type-id of an alias-declaration (7.1.3) that is not the declaration of a template-declaration. 除非声明构造函数,析构函数或转换函数,否则声明中至少需要一个不是cv-qualifier的类型说明符.92 type-specifier-seq不应定义类或枚举,除非它出现在类型中 - 别名声明(7.1.3)的id,它不是模板声明的声明。

where the part of particular interest is that A type-specifier-seq shall not define a class or enumeration unless... 其中特别感兴趣的部分是A type-specifier-seq shall not define a class or enumeration unless...

From N3797: 从N3797:

8.3.5/9 Types shall not be defined in return or parameter types. 8.3.5 / 9不应在返回或参数类型中定义类型。 The type of a parameter or the return type for a function definition shall not be an incomplete class type (possibly cv-qualified) unless the function is deleted ( 8.4.3 ) or the definition is nested within the member-specification for that class (including definitions in nested classes defined within the class). 函数定义的参数类型或返回类型不应是不完整的类类型(可能是cv限定的),除非删除该函数(8.4.3)或定义嵌套在该类的成员规范中(包括在类中定义的嵌套类中的定义。

This blocks defining new types in a function declaration. 这会阻止在函数声明中定义新类型。

The next two are other corner cases not mentioned by the OP: 接下来的两个是OP未提及的其他角落案例:

11.3/2 A class shall not be defined in a friend declaration. 11.3 / 2不应在朋友声明中定义一个班级。

14.1/2 Types shall not be defined in a template-parameter declaration. 14.1 / 2类型不应在模板参数声明中定义。

Finally, this clause blocks it in sizeof and elsewhere: 最后,这个子句在sizeof和其他地方阻止它:

7.1.6/3 A type-specifier-seq shall not define a class or enumeration unless it appears in the type-id of an alias-declaration (7.1.3) that is not the declaration of a template-declaration 7.1.6 / 3 type-specifier-seq不应定义类或枚举,除非它出现在别名声明(7.1.3)的type-id中,而不是模板声明的声明

Note that C does not have that restriction (C.1.4) 请注意,C没有该限制(C.1.4)

In addition, in a previous version of the C++ standard, we had: 此外,在以前版本的C ++标准中,我们有:

5.3.3p5 Types shall not be defined in a sizeof expression 5.3.3p5类型不应以sizeof表达式定义

which I cannot find in the most recent version standard proposals, and it is redundant under N3797 as sizeof s route to define a type in the grammar is via type-specifier-seq , and blocked by 7.1.6/3: 我在最新版本的标准提案中找不到它,并且在N3797下是冗余的,因为sizeof的路由在语法中定义类型是通过type-specifier-seq ,并被7.1.6 / 3阻塞:

sizeof(type-id) -> type-id -> type-specifer-seq -> type-specifier -> class-specifier sizeof(type-id) - > type-id - > type-specifer-seq - > type-specifier - > class-specifier

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM