如何使用指定数量的元素(相同类型)创建boost :: tuple?

 手机用户2502931221 发布于 2023-02-08 19:27

假设我有以下类定义:

template 
class foo
{
    boost::tuples::tuple<...> bar;
};

给定编译时常量N,我想将类型扩展为bar包含N指定类型元素的元组.也就是说,类型foo<2>::barboost::tuples::tuple.我猜我可以使用Boost.MPL,但我还没有想出确切的序列.我想我能做到:

template 
struct type_repeater
{
    typedef typename boost::mpl::fold<
        boost::mpl::range_c,
        boost::mpl::vector<>,
        boost::mpl::push_back<_1, T>
    >::type type;
};

所以那么例如type_repeater::type相当于boost::mpl::vector.我只是不确定如何/如果我可以采取该类型列表并将其注入元组的参数列表,就像我想要的那样.这可能吗?

1 个回答
  • 这似乎是使用C++ 11的一个很好的最小例子

    #include <tuple>
    template <unsigned int N, typename T>
    struct type_repeater {
      typedef decltype(std::tuple_cat(std::tuple<T>(), typename type_repeater<N-1, T>::type())) type;
    };
    
    template <typename T>
    struct type_repeater<0, T> {
      typedef decltype(std::tuple<>()) type;
    };
    
    int main() {
      type_repeater<3, float>::type asdf;
      std::get<0>(asdf);
      std::get<1>(asdf);
      std::get<2>(asdf);
    }
    

    2023-02-08 19:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有