C ++ 1y / C ++ 14:将静态constexpr数组转换为非类型模板参数包? [英] C++1y/C++14: Converting static constexpr array to non-type template parameter pack?

查看:104
本文介绍了C ++ 1y / C ++ 14:将静态constexpr数组转换为非类型模板参数包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个constexpr数组(已知的bound)的静态存储持续时间:

  constexpr T input [] = * ... * /; 

我有一个输出类模板需要一个包:

 模板< T ...> struct output_template; 



我想实例化 output_template like:使用output = output_template< input [0],input [1],...,input [n-1]>输入/ p>

  ; 

一种方法是:

 模板< 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>()));
};

使用output = make_output_template< std :: extent_v< decltype(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;


Suppose I have a constexpr array (of known bound) of static storage duration:

constexpr T input[] = /* ... */;

And I have an output class template that needs a pack:

template<T...> struct output_template;

I want to instantiate output_template like:

using output = output_template<input[0], input[1], ..., input[n-1]>;

One way to do this is:

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;

Is there a cleaner or simpler solution I am missing?

解决方案

Maybe you consider this to be cleaner:

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 ]... >;
};

with

using output = make_output_template<
    input,
    std::make_index_sequence< std::extent_v< decltype( input ) > >
>::type;

这篇关于C ++ 1y / C ++ 14:将静态constexpr数组转换为非类型模板参数包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆