Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In C++11 a type specifier includes class specifiers and enum specifiers. (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?

like image 907
Andrew Tomazos Avatar asked Jan 07 '14 19:01

Andrew Tomazos


2 Answers

The reason it can't be found in the C++ standard is because it's actually prohibited in a delta from the C standard.

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. which shows the prohibition in question.

This is explicitly called out in 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.

where the part of particular interest is that A type-specifier-seq shall not define a class or enumeration unless...

like image 60
Mark B Avatar answered Oct 08 '22 23:10

Mark B


From N3797:

8.3.5/9 Types shall not be defined in return or parameter types. 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).

This blocks defining new types in a function declaration.

The next two are other corner cases not mentioned by the OP:

11.3/2 A class shall not be defined in a friend declaration.

14.1/2 Types shall not be defined in a template-parameter declaration.

Finally, this clause blocks it in sizeof and elsewhere:

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

Note that C does not have that restriction (C.1.4)

In addition, in a previous version of the C++ standard, we had:

5.3.3p5 Types shall not be defined in a sizeof expression

which I cannot find in the most recent version standard proposals, and it is redundant under N3797 as sizeofs route to define a type in the grammar is via type-specifier-seq, and blocked by 7.1.6/3:

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

like image 36
Yakk - Adam Nevraumont Avatar answered Oct 09 '22 00:10

Yakk - Adam Nevraumont