注入的类名编译器差异

2024-06-28

考虑这段代码:

struct foo{};

int main() {
    foo::foo a;
}

我希望这是格式良好的,声明类型的变量foo根据 [class]/2 中的规则(N4140,重点是我的):

A 班级名称被插入到紧随其声明之后的作用域中班级名称被看到。The 班级名称也被插入到类本身的范围中;这被称为注入类名。 出于访问检查的目的,注入的类名称被视为公共成员名称。

clang 3.6.0 http://coliru.stacked-crooked.com/a/3f2622297cbb99e6同意我的观点,编译上述代码时没有适用的警告-Wall -pedantic.

gcc 5.2.0 http://coliru.stacked-crooked.com/a/e507bb6594ccae93不同意,提供以下错误消息:

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在这种情况下是错误的。我正在寻找的相关异常在 [class.qual]/2 中:

2 在不忽略函数名称且嵌套名称说明符指定类 C 的查找中:

  • (2.1) 如果在 C 中查找时,在嵌套名称说明符之后指定的名称是 C 的注入类名称, or

  • [...]

相反,该名称被视为命名类 C 的构造函数。

该标准有一个几乎等效的(显然是非规范的)示例:

struct A { A(); };
struct B: public A { B(); };

A::A() { }
B::B() { }

B::A ba;// object of type A
A::A a;// error, A::A is not a type name
struct A::A a2;// object of type A

然而,clang在这种情况下实际上发出了正确的诊断:

error: qualified reference to 'A' is a constructor name rather than a type wherever a constructor can be declared

Perhaps clang解释该行In a lookup in which function names are not ignored as In a lookup in which a constructor declaration is valid,但这似乎不是一个正确的解释。

有一个现有的错误 https://llvm.org/bugs/show_bug.cgi?id=13403为此在clang巴吉拉。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

注入的类名编译器差异 的相关文章

随机推荐