班级名称注入和构造函数

Class Name Injection and Constructors

本文关键字:构造函数 注入      更新时间:2023-10-16

最近,将库更新为clang 5.x时,我注意到我的代码中的一个错误,该错误先前在Clang 4.x,GCC 5.x-6.x和MSVC 2015和2017。

#include <iostream>
#include <typeinfo>
#include <vector>
int main()
{
    using a = typename std::vector<int>::vector;
    std::cout << typeid(a).name() << std::endl;
    return 0;
}

clang-5.x会产生以下警告消息,而所有其他编译器则默默地编译上述代码:

a.cpp:7:42: warning: ISO C++ specifies that qualified reference to 'vector' is a
  constructor name rather than a type in this context, despite preceding
  'typename' keyword [-Winjected-class-name]
using a = typename std::vector<int>::vector;

哪个编译器有货物?我是正确的,假设Clang5.x在这里具有正确的行为,并且所有其他编译器(和版本)都是不正确的。如果是这样,这是否值得向MSVC和GCC提交错误报告?

clang-5非常正确。在[class.qual]/2:

在查找中,函数名称不忽略,并且 Nested-name-specifier 提名类C

  • 如果在嵌套名称指示符之后指定的名称,在C中抬头时,是C
  • 的注入式名称。
  • ...

该名称被视为命名C 的构造函数。

至于问题的另一部分。是的,提交错误报告绝对值得。要鼓励IMO标准合规性(或至少对其进行更多诊断)。