根据($ 3.4.4)类型名称后跟一个类型密钥无效。 但我不确定哪个范围? 例如:在下面,如果在块中使用详细说明符(如函数内部),编译器不会抱怨。
typedef class { /* ... */ } S; // invalid class S; // ok void foo() { class S; }使用typedef-name在本地范围内声明一个类是否有效,为什么?
According to ($ 3.4.4) A typedef name followed by a class-key is invalid. But I'm not sure about which scope? For example: In the following, compiler doesn't complain if elaborated specifier was used in a block such as inside a function.
typedef class { /* ... */ } S; // invalid class S; // ok void foo() { class S; }Is it valid to declare a class inside a local scope with typedef-name, Why?
最满意答案
7.1.3第3段说明:
class complex { /* ... */ }; typedef int complex; //在给定范围内,typedef说明符不得用于重新定义在thascope中声明的任何类型的名称以引用其他类型。 [例:
错误:重新定义
然后它去:
typedef int complex; class complex { /* ... */ };-end示例]类似地,在给定范围内,类或枚举不应声明与在该范围内声明的typedef-name相同的名称,并且引用类或枚举本身以外的类型。 [例:
//错误:重新定义
这是你的榜样。
7.1.3 paragraph 3 tells :
class complex { /* ... */ }; typedef int complex; //In a given scope, a typedef specifier shall not be used to redefine the name of any type declared in thascope to refer to a different type. [Example:
error: redefinition
Then it goes :
typedef int complex; class complex { /* ... */ };—end example] Similarly, in a given scope, a class or enumeration shall not be declared with the same name as a typedef-name that is declared in that scope and refers to a type other than the class or enumera- tion itself. [Example:
// error: redefinition
This is your example.
更多推荐
发布评论