模板构造函数在 MSVC 中失败,因为成员函数与参数类型的名称冲突
Template constructor fails in MSVC due to name collision of member function with argument type
以下代码截图在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
。
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类