【发布时间】:2019-02-02 21:55:24
【问题描述】:
以下代码编译正常:
struct A {
int i;
constexpr A() : i(1) { }
constexpr A(const A& that) : i(1) { }
};
constexpr auto func() {
std::array<A, 3> result = {};
return result;
}
但是,如果我们给A添加一个模板类型参数T,
template<typename T> struct A {
int i;
constexpr A() : i(1) { }
constexpr A(const A<T>& that) : i(1) { }
};
constexpr auto func() {
std::array<A<int>, 3> result = {};
return result;
}
编译器错误“constexpr function 'func' cannot result in an constant expression”。
这怎么可能?
【问题讨论】:
-
哪个编译器?在Coliru 上,它编译得很好。
-
编译器:Microsoft Visual C++ 14.16.27023