【发布时间】:2021-05-24 03:05:59
【问题描述】:
以下代码在 gcc 9.3 中编译,但在 gcc 10.2 中不编译:
constexpr std::array<int, 2> opt = {1,2};
template <typename T>
constexpr auto f(const T& arr)
{
std::array<int, arr.size()> res{};
return res;
}
int main()
{
auto res = f(opt);
}
代码在https://godbolt.org/z/8hb6M8。
gcc10.2给出的错误是arr.size() is not a constant expression。
哪个编译器是正确的? 9.3 还是 10.2?
如果 10.2 是正确的,我如何定义编译时数组并将其大小(和数组)作为参数传递?
【问题讨论】:
标签: c++ gcc constexpr c++20 stdarray