C++1y/C++14 : Converting static constexpr array to non-type template parameter pack?

标签 c++ templates variadic-templates constexpr c++14

假设我有一个静态存储持续时间的 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;

关于C++1y/C++14 : Converting static constexpr array to non-type template parameter pack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24560547/

相关文章:

c++ - 正在等待条件变量、正在加入的线程会发生什么情况?

c++ - 继承与特化

javascript - 推荐用于 JQuery 的 JavaScript HTML 模板库?

c++ - 特定成员函数的部分特化

C++ - 可变参数函数的多个参数包

c++ - 'CommandList::DrawInstanced' 如何选择多个槽中的顶点缓冲区之一?

c++ - 无法解决将二维数组传递给函数的外部问题

c++ - 根据参数数量从函数返回结构对象并将其分配给一般结构的对象

c++ - 如何有条件地在具有相同签名的两个构造函数之间切换?

c++ - 模板参数包如何同时具有显式参数和推导参数?