方法的声明更改了符号的含义 [英] Declaration of method changes meaning of symbol
问题描述
对于以下代码:
struct foo {};
struct A
{
typedef foo foo_type;
void foo();
};
GCC给出了编译器错误:
GCC gives a compiler error:
test.cpp:7:14: error: declaration of 'void A::foo()' [-fpermissive]
void foo();
^
test.cpp:1:8: error: changes meaning of 'foo' from 'struct foo' [-fpermissive]
struct foo {};
^
但是clang接受它而没有编译器错误。谁是对的?
But clang accepts it without compiler errors. Who is right?
请注意,如果删除了typedef,或将其更改为 typedef :: foo foo_type
, gcc和clang接受代码。
Note that if the typedef is removed, or changed to typedef ::foo foo_type
, both gcc and clang accept the code.
推荐答案
gcc是正确的,但不需要clang提供诊断(3.3.7) :
gcc is correct, but clang is not required to give a diagnostic (3.3.7):
在类S中使用的名称N应在
上下文中引用相同的声明,并在S的完整范围。违反此规则无需
诊断。
A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.
这是因为分类范围工程。 void foo();
的 foo
在该类的 entire 范围内可见 A
,因此 void foo();
的声明更改了 foo ,从引用
struct foo
到函数 foo
的名称。
This is because of how class scope works. The foo
of void foo();
is visible within the entire scope of the class A
, so the declaration of void foo();
changes the meaning of foo
in the typedef from referring to struct foo
to the name of the function foo
.
这篇关于方法的声明更改了符号的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!