首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用boost::fusion::fold将boost::fusion::设置为boost::fusion::map

使用boost::fusion::fold将boost::fusion::设置为boost::fusion::map
EN

Stack Overflow用户
提问于 2011-07-17 02:57:43
回答 1查看 902关注 0票数 2

我有一个融合集,并想将其转换为融合地图。

代码语言:javascript
复制
#include <cstdlib>
#include <iostream>

#include <boost/fusion/include/fold.hpp>
#include <boost/fusion/include/map.hpp>
#include <boost/fusion/include/make_map.hpp>
#include <boost/fusion/include/push_back.hpp>
#include <boost/fusion/include/pair.hpp>
#include <boost/fusion/container/set.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/at_key.hpp>

struct node_base
{
    int get() {return 123;}
};
struct node_a : public node_base
{
node_a(){std::cout << "node_a::Ctor"<< std::endl;}
};
struct node_b : public node_base
{
node_b(){std::cout << "node_b::Ctor"<< std::endl;}
};
struct node_c : public node_base
{
node_c(){std::cout << "node_c::Ctor"<< std::endl;}
};

typedef ::boost::fusion::set<node_a,node_b,node_c> my_fusion_set;
my_fusion_set my_set;

struct push_back_map
{
    typedef ::boost::fusion::map<> result_type;

    template<class NODE>
    ::boost::fusion::map<> operator() (const ::boost::fusion::map<> & m, const NODE & n)
    {
    return ::boost::fusion::push_back(
        m
        , ::boost::fusion::make_pair<NODE>(n)
        );
    }
};

::boost::fusion::map<> my_map =
::boost::fusion::fold(
    my_set
    , ::boost::fusion::map<>()
    , push_back_map()
    );

struct print_map
{
    template<class P>
    void operator()(P & p) const
    {
    std::cout << p.second.get() << std::endl;
    }
};

int main()
{
    ::boost::fusion::for_each(my_map, print_map());
    std::cout << ::boost::fusion::at_key<node_a>(my_set).get() << std::endl;
    //std::cout << ::boost::fusion::at_key<node_a>(my_map).second.get() << std::endl;
    system("pause");
    return 0;
}

这段代码会编译。但是它不能打印出my_map的结果。my_set构建成功。my_map只是从一个my_set转换而来,每个元素的原始类型作为键,默认构造函数中的实例对象作为值。我想知道my_map是否能从fusion::fold成功创建。my_set可以通过调用fusion::at_key<>进行查询,但my_map不适用于at_key<>。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2011-07-17 06:10:54

我想不出一种简洁的方法来用foldfusion::set创建fusion::map。所以这个答案不会直接解决你的问题。我把这篇文章贴出来,以防它能给你一些提示。

准备一个类型列表(如下面的my_types ),并从该列表创建fusion::setfusion::map,而不是直接从fusion::set创建fusion::map,怎么样?

例如:

代码语言:javascript
复制
namespace bf = boost::fusion;
namespace bm = boost::mpl;

typedef bm::vector<node_a,node_b,node_c> my_types;
typedef bm::fold<
          my_types
        , bm::vector<>
        , bm::push_back< bm::_1, bf::pair< bm::_2, bm::_2 > >
        >::type my_map_types;

int main() {
    bf::result_of::as_set<my_types>::type my_set;
    bf::result_of::as_map<my_map_types>::type my_map;
    std::cout << bf::at_key<node_a>(my_set).get() << std::endl;
    std::cout << bf::at_key<node_a>(my_map).get() << std::endl;
}

这是一个关于ideone的测试。顺便说一句,如果您的fusion::map中的键类型和值类型总是相同的,那么fusion::set就能达到这个目的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6719514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档