模板构造函数在 MSVC 中失败,因为成员函数与参数类型的名称冲突

Template constructor fails in MSVC due to name collision of member function with argument type

本文关键字:参数 函数 类型 冲突 成员 因为 构造函数 MSVC 失败      更新时间:2023-10-16

以下代码截图在MSVC 18.00.31101中编译失败,并出现以下错误,但在gcc 4.9.2和clang 3.6.0中成功。限定参数类型范围或在声明中包含 struct 关键字可解决错误。这是编译器错误还是未定义的行为?

#include <cstdlib>
struct A {
    int B;
};
struct Snap {
    template<size_t TSize>
    Snap(const A (&)[TSize]) {
        // do something with TSize
    }
    void A() {}
};
int main() {
    A pop[] = { {1}, {2}, {3} };
    Snap crackle(pop);
    return 0;
}

.

1> <...>: error C2664: 'Snap::Snap(const Snap &)' : cannot convert argument 1 from 'A [3]' to 'const Snap &'
1>          Reason: cannot convert from 'A [3]' to 'const Snap'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

这是格式不正确的,但不需要诊断。[basic.scope.class]/p1:

2) 类N中使用的名称S应指代 它的上下文以及在已完成的S范围内重新评估时。不 违反此规则需要诊断。

在其上下文中A评估的名称是指::A,但在已完成的Snap范围内重新计算时指的是Snap::A