jjzjj

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

coder 2024-02-10 原文

假设我有一个静态存储持续时间的 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++1y/C++14 : Converting static constexpr array to non-type template parameter pack?的更多相关文章

随机推荐