考虑这段代码:
#include <array>
template < int... Ints >
constexpr std::array<int,sizeof...(Ints)> theIntArray = {Ints...};
template < size_t NN >
constexpr void test(const std::array<int,NN>& xx)
{
theIntArray<xx[0]>;
}
constexpr std::array<int,2> aa = {10,20};
int main()
{
theIntArray<aa[0]>; // passes
test(aa); // FAILS ?!
return 0;
}
在main()
函数中,第一行通过,而第二行失败并出现奇怪的错误消息:
error: ‘* & xx’ is not a constant expression
note: in template argument for type ‘int’
我正在使用 gcc-7.0.1,您可以在此处找到实时示例。
这是根据标准还是错误?是什么让第二行失败而第一行通过?