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

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

概述我想使用c 14可变参数模板构建编译时查找表. 目前我在那里: static const unsigned kCount = 5;template<unsigned Index>constexpr auto getRow(void){ return std::array<unsigned, 2> { Index, Index * Index };}template<unsig 我想使用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>()
总结

以上是内存溢出为你收集整理的C 14使用可变参数模板编译时间std :: array全部内容,希望文章能够帮你解决C 14使用可变参数模板编译时间std :: array所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1221523.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存