gpt4 book ai didi

c - 为什么允许多次声明 typedef 标识符?

转载 作者:太空狗 更新时间:2023-10-29 16:40:48 26 4
gpt4 key购买 nike

来自 C99 标准,6.7(5):

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that: for an object, causes storage to be reserved for that object; for a function, includes the function body; for an enumeration constant or typedef name, is the (only) declaration of the identifier.

如果带有 typedef 的标识符实际上是定义,那么为什么允许它们被声明多次?示例:

int main()
{
typedef int x;
typedef int x;
}

以上程序编译没有错误。这怎么可能?我原以为程序会给我一个多重定义错误。

最佳答案

C99与C11不同

规则在 C99 和 C11 之间发生变化(据我所知,C11 规则与 C++ 规则相匹配)。请注意,在这两个标准中,¶3 在约束部分,而¶5 在语义部分。这对于错误消息很重要——约束违规需要诊断。

ISO/IEC 9899:1999 §6.7 Declarations

¶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.

5 A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

  • for an object, causes storage to be reserved for that object;
  • for a function, includes the function body;98)
  • for an enumeration constant or typedef name, is the (only) declaration of the identifier.

ISO/IEC 9899:2011 §6.7 Declarations

¶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 that:

  • a typedef name may be redefined to denote the same type as it currently does, provided that type is not a variably modified type;
  • tags may be redeclared as specified in 6.7.2.3.

¶5 A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

  • for an object, causes storage to be reserved for that object;
  • for a function, includes the function body;119)
  • for an enumeration constant, is the (only) declaration of the identifier;
  • for a typedef name, is the first (or only) declaration of the identifier.

Lundin noted标准 C 委员会的 web site包含 n1360 ,一个单页文档,详细说明了进行此更改的原因。基本上:C++ 做到了;一些编译器已经这样做了;允许(要求)它既不难做也不颠覆任何事情。

GCC 并不总是强制执行标准

如果您的代码正在编译,那么它是在 C11 规则或 C++ 规则下编译的。它不是在(严格的)C99 规则下编译的。

请注意,足够新的 GCC 版本允许重新定义,除非您坚持不这样做,但也要注意 report通过 John Bollinger GCC 4.4.7(在一个身份不明的平台上)根本不允许在 C99 模式下重新定义。

考虑文件 retypedef.c:

int main(void)
{
typedef int x;
typedef int x;
x y = 0;
return y;
}

使用 GCC 4.9.1 在 Mac OS X 10.9.5 上编译,我得到:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror           -c retypedef.c
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -pedantic -c retypedef.c
$ gcc -O3 -g -std=c99 -Wall -Wextra -Werror -c retypedef.c
$ gcc -O3 -g -std=c99 -Wall -Wextra -Werror -pedantic -c retypedef.c
retypedef.c: In function ‘main’:
retypedef.c:4:17: error: redefinition of typedef ‘x’ [-Werror=pedantic]
typedef int x;
^
retypedef.c:3:17: note: previous declaration of ‘x’ was here
typedef int x;
^
cc1: all warnings being treated as errors
$

它不会提示,除非使用 -pedantic,然后只有在请求 C99 时(在 C11 的相同范围内重新定义 typedef 是符合标准的).

关于c - 为什么允许多次声明 typedef 标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26240370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com