对于以下代码:
struct foo {};
struct A
{
typedef foo foo_type;
void foo();
};
GCC 给出编译器错误:
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 接受它而没有编译器错误。谁是对的?
请注意,如果 typedef 被删除,或更改为 typedef::foo foo_type
,gcc 和 clang 都会接受该代码。
最佳答案
gcc 是正确的,但 clang 不需要给出诊断(3.3.7):
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
在类 A
的整个 范围内可见,因此声明void foo();
将 typedef 中 foo
的含义从引用 struct foo
更改为函数的名称 foo
.
关于c++ - 方法的声明改变了符号的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15537023/