Using the typedef name in the developed specifier

According to ($ 3.4.4) the typedef name followed by the class key is invalid. But I'm not sure in which area? For example: The following compiler does not complain if the block uses a built-in qualifier, for example, inside a function.

typedef class { /* ... */ } S; // invalid class S; // ok void foo() { class S; } 

Is it possible to declare a class inside a local scope named typedef, Why?

+4
source share
3 answers

7.1.3 paragraph 3 states:

In a given scope, the typedef specifier should not be used to override the name of any type declared in thascope, to indicate another type. [Example:

 class complex { /* ... */ }; typedef int complex; // 

error: override

Then he goes:

-end example] Similarly, in a given scope, a class or enumeration should not be declared with the same name as the typedef name that is declared in this scope and is of a type other than the class or the enumeration itself. [Example:

 typedef int complex; class complex { /* ... */ }; 

// error: override

This is your example.

+5
source

The problem is that you declared the class without a name, but with an alias (typedef). Later, you used the same name to declare without defining another class (I know that it was not an intention, but that the compiler understood it), and its name ran into typedef. When you did the same inside foo (), it was a split region, and that was acceptable. But note that the β€œclass S” inside foo () is NOT the same type as the first line.

+2
source

Outside of a function, you cannot declare a class with the same name as typedef in the same namespace.

Inside the function, you declare a new class, limited inside the function. This is not the same as an anonymous class declared in the surrounding namespace. Inside the function, this hides the typedef declaration.

0
source

Source: https://habr.com/ru/post/1383870/


All Articles