[英]C++1y/C++14: Converting static constexpr array to non-type template parameter pack?
假设我有一个静态存储持续时间的constexpr数组(已知绑定):
constexpr T input[] = /* ... */;
我有一个需要包的输出类模板:
template<T...> struct output_template;
我想实例化output_template
如:
using output = output_template<input[0], input[1], ..., input[n-1]>;
一种方法是:
template<size_t n, const T (&a)[n]>
struct make_output_template
{
template<size_t... i> static constexpr
output_template<a[i]...> f(std::index_sequence<i...>)
{ return {}; };
using type = decltype(f(std::make_index_sequence<n>()));
};
using output = make_output_template<std::extent_v<decltype(input)>, input>::type;
我缺少更清洁或更简单的解决方案吗?
也许你认为这更清洁:
template< const T* a, typename >
struct make_output_template;
template< const T* a, std::size_t... i >
struct make_output_template< a, std::index_sequence< i... > >
{
using type = output_template< a[ i ]... >;
};
同
using output = make_output_template<
input,
std::make_index_sequence< std::extent_v< decltype( input ) > >
>::type;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.