在详细说明符中使用typedef名称(Use of typedef name in elaborated specifier)

编程入门 行业动态 更新时间:2024-10-24 22:20:25
在详细说明符中使用typedef名称(Use of typedef name in elaborated specifier)

根据($ 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段说明:

在给定范围内,typedef说明符不得用于重新定义在thascope中声明的任何类型的名称以引用其他类型。 [例:

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

错误:重新定义

然后它去:

-end示例]类似地,在给定范围内,类或枚举不应声明与在该范围内声明的typedef-name相同的名称,并且引用类或枚举本身以外的类型。 [例:

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

//错误:重新定义

这是你的榜样。

7.1.3 paragraph 3 tells :

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:

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

error: redefinition

Then it goes :

—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:

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

// error: redefinition

This is your example.

更多推荐

本文发布于:2023-07-24 10:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:说明符   名称   详细   typedef   elaborated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!