【发布时间】:2017-11-05 21:09:19
【问题描述】:
可以将 char 类型的值模板参数包解压缩为(编译时)字符串。
如何将string_view 获取到该字符串中?
我想做什么:
int main()
{
constexpr auto s = stringify<'a', 'b', 'c'>();
constexpr std::string_view sv{ s.begin(), s.size() };
return 0;
}
试试:
template<char ... chars>
constexpr auto stringify()
{
std::array<char, sizeof...(chars)> array = { chars... };
return array;
}
错误:
15 : <source>:15:30: error: constexpr variable 'sv' must be initialized by a constant expression
constexpr std::string_view sv{ s.begin(), s.size() };
^~~~~~~~~~~~~~~~~~~~~~~~~
15 : <source>:15:30: note: pointer to subobject of 's' is not a constant expression
有没有办法获得main 函数中的行为?
【问题讨论】:
标签: c++ variadic-templates c++17 string-view