考虑以下代码:电话constexpr函数接受的阵列编译失败
#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,你可以找到现场示例here。
这是根据标准还是它是一个错误?第一条线经过时,什么使第二条线失败?
TIL将常量引用添加到文字类型以模仿用户定义的非类型模板参数。 upvoted。 – TemplateRex