重复的typedef - 无效的C,但有效的C ++? [英] Repeated typedefs - invalid in C but valid in C++?

查看:773
本文介绍了重复的typedef - 无效的C,但有效的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个标准的参考,为什么下面的code触发在C遵守警告(与 GCC -pedantic 测试;重新定义的typedef),但罚款C ++( G ++ -pedantic

I would like a standard reference why the following code triggers a compliance warning in C (tested with gcc -pedantic; "typedef redefinition"), but is fine in C++ (g++ -pedantic):

typedef struct Foo Foo;
typedef struct Foo Foo;

int main() { return 0; }

为什么不能定义一个的typedef 反复使用C?

(这对于的<一个头结构的实际影响href=\"http://sc2.svn.sourceforge.net/viewvc/sc2/trunk/sc2/src/libs/resource/index.h?revision=3704&view=markup\">C项目。)

推荐答案

由于C ++标准明确地这样说。

Why does this compile in C++?

Because the C++ Standard explicitly says so.

参考:

C ++标准03 7.1.3的typedef符

§7.1.3.2:

在给定的非类范围,一个typedef符可以用来重新定义在该范围内声明引用其所已经提到的类型的任何类型的名称。

In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers.

[示例:搜索
  的typedef的struct {/ * ... * /} S;结果
  的typedef INT I;结果
  的typedef INT I;结果
  我的typedef我;结果
  例如末端]

[Example:
typedef struct s { /* ... */ } s;
typedef int I;
typedef int I;
typedef I I;
—end example]

的typedef 名没有联系和C99标准禁止标识,没有链接规范有一个以上的声明具有相同的范围,并在相同的名称空间。

Why does this fail to compile in C?

typedef names have no linkage and C99 standard disallows identifiers with no linkage specification to have more than one declaration with the same scope and in the same name space.

参考:

C99标准:§6.2.2鉴定连接器的联系

§6.2.2/ 6 规定:

以下鉴定连接器没有连接的:一个鉴定音响器宣称比其他任何东西
  一个对象或一个功能;声明的鉴定连接器是一个函数参数;
块范围
  声明没有存储类特定网络erextern为对象鉴定连接器。

The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifierextern.

另外的§6.7/ 3 规定:

如果标识符没有联动,应当有该标识符与相同的范围,并在同一名称空间不超过一个声明(在声明符或类型说明符),除标签在6.7.2.3规定。

If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except for tags as specified in 6.7.2.3.

这篇关于重复的typedef - 无效的C,但有效的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆