#include <array>
#include <tuple>
typedef std::tuple<const int> TupleType;
constexpr std::array<const int, 2> a = {1, 2};
constexpr void foo()
{
for (std::size_t i = 0; i < a.size(); ++i)
{
const int j = i;
typedef std::tuple_element<j, TupleType> T;
}
}
该代码不能被GCC-7.2编译--std = C++ 17与以下编译错误:编译时间类型代constexpr功能
error: the value of 'j' is not usable in a constant expression
note: in template argument for type 'long unsigned int'
如果我们假设函数(和相应的循环)在编译时被评估(这对于从C++ 14开始的循环是可行的),为什么这个代码不能尽管i未被声明为const,但它实际上可以是constexpr,因为它的所有值在编译时也是已知的。
您能否澄清一下这段代码是否因其思想而无效?或者有一个编译器限制?或者以下都不是?