【发布时间】:2015-11-07 11:27:10
【问题描述】:
考虑这段代码:
struct foo{};
int main() {
foo::foo a;
}
我希望这是格式正确的,按照 [class]/2(N4140,强调我的)中的规则声明 foo 类型的变量:
class-name 被插入到在看到 class-name 之后立即声明它的范围内。 类名也被插入到类本身的作用域中;这被称为 injected-class-name。 出于访问检查的目的,注入的类名被视为公共成员名。
clang 3.6.0 同意我的看法,编译上面的代码没有适用的警告-Wall -pedantic。
gcc 5.2.0 不同意,提供以下错误消息:
main.cpp: In function 'int main()':
main.cpp:5:5: error: 'foo::foo' names the constructor, not the type
foo::foo a;
无论注入的类名的嵌套有多深,上述内容都成立,例如foo::foo::foo::foo.
是否有规则强制该构造在该上下文中被解释为构造函数,或者这是一个gcc 错误?还是我对标准引用的解释有误?
【问题讨论】:
-
Clang 在处理注入类名时存在许多错误。这是其中之一。
标签: c++ gcc clang language-lawyer c++14