【发布时间】:2021-06-16 19:32:44
【问题描述】:
起源问题是我想在模板非类型参数中使用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]”对于编译时是可以的。我无法将其存储在值中或将其传递给其他函数。
【问题讨论】:
标签: c++ constexpr constexpr-function