考慮以下代碼:電話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