起源问题是我想在模板非类型参数中使用const char*
或。char []
当然现在不支持了。所以我想写一些代码来转换char[]
为std::integer_sequence
. 但是我发现了一个严重的问题。
#include<utility>
template<typename T, std::size_t n>
constexpr auto f3(T (&a)[n])
{
return std::integer_sequence<T,a[0]>(); //simplified, it should be <T, a[i],...>
}
template<typename T, std::size_t n>
constexpr auto f2(T (&a)[n])
{
constexpr T v=a[3];
//.....other code
return v;
}
template<typename T, std::size_t n>
constexpr auto f1(T (&a)[n])
{
return a[3];
}
int main()
{
constexpr char a[]="abcdefg";
constexpr auto v1=f1(a);
//constexpr auto v2=f2(a);
//constexpr auto v3=f3(a);
}
f1 没问题,但 f2 和 f3 是错误的。我很困惑......为什么会这样?看起来只有“return xxx[yyy]”对于编译时是可以的。我无法将其存储在值中或将其传递给其他函数。