加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 14使用可变参数模板编译时间std :: array

发布时间:2020-12-16 10:36:54 所属栏目:百科 来源:网络整理
导读:我想使用c 14可变参数模板构建编译时查找表. 目前我在那里: static const unsigned kCount = 5;templateunsigned Indexconstexpr auto getRow(void){ return std::arrayunsigned,2 { Index,Index * Index };}templateunsigned... Indicesconstexpr auto gen
我想使用c 14可变参数模板构建编译时查找表.
目前我在那里:

static const unsigned kCount = 5;

template<unsigned Index>
constexpr auto getRow(void)
{
    return std::array<unsigned,2> { Index,Index * Index };
}

template<unsigned... Indices>
constexpr auto generateTable(std::index_sequence<Indices...>)
{
    return std::array<std::array<unsigned,2>,sizeof...(Indices)>
    {
        // This is were I'm stuck. How to build a std::array using Indices as template parameter in getRow()?
    };
}

constexpr auto generate(void)
{
    return generateTable(std::make_index_sequence<kCount>{});
}

我希望表在std :: array中.每行包含一个带有2列的std :: array.我陷入了generateTable(),我需要以某种方式将我的Indices传递给getRow()作为模板参数.

这可以使用std :: integer_sequence和模板参数包扩展来实现,还是我需要自己实现递归?

(getRow()被简化 – 值类型实际上来自模板类型.索引*索引只是一个占位符.我需要知道如何使用参数包扩展调用getRow().)

解决方法

看起来你几乎就在那里.只需依靠参数包扩展:

return std::array<std::array<unsigned,sizeof...(Indices)>
{
   getRow<Indices>()...
};

其中getRow< Indices>()…行将扩展为:

getRow<0>(),getRow<1>(),.....,getRow<sizeof...(Indices)-1>()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读