【发布时间】:2015-04-25 01:56:18
【问题描述】:
以下代码片段无法在 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
【问题讨论】:
-
您可以将 clang 3.6.0 添加到可以工作的编译器列表中。 See it live
-
只是好奇,如果你写出
Snap(const struct A (&)[TSize]),那么MSVC会编译它吗? -
是的,这也解决了。
标签: c++ templates visual-c++