【发布时间】:2019-09-11 17:05:24
【问题描述】:
下面的代码在 MSVC 上编译,但在 GCC (4.6.3) 上失败。为什么会失败,我应该如何解决?
#include <array>
class Foo {
public:
template<typename T, int N>
operator std::array<T, N>() const {
return std::array<T, N>();
}
};
int main(){
Foo val;
// both of the following lines fail on GCC with error:
// "no matching function call...", ultimately with a note:
// "template argument deduction/substitution failed"
auto a = val.operator std::array<int, 2>();
static_cast<std::array<int, 2>>(val);
return 0;
}
编辑:尽管为std::array 的模板参数传递了int,但以下代码确实可以编译(在两个编译器上)。
template<int N, typename T>
struct Bar {
std::array<T, N> buf;
};
int main()
{
auto x = Bar<3, double>();
return 0;
}
【问题讨论】:
标签: c++ templates gcc compiler-errors c++14