使用boost::fusion::fold将boost::fusion::set转换为boost::fusion::ma
convert boost::fusion::set to boost::fusion::map by using boost::fusion::fold
我有一个融合集,想把它转换成一个融合图。
#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<>。谢谢!
我想不出一个简洁的方法来创建fusion::map
fusion::set
和fold
。所以这个答案不能直接解决你的问题。我把这个贴出来是为了给你一些提示。
如何准备一个类型列表(如下my_types
),以及从列表中创建fusion::set
和fusion::map
而不是直接从fusion::set
创建fusion::map
?例如:
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
也可以达到这个目的。
相关文章:
- 将"boost::tuple "转换为"boost::fusion::tuple&quo
- 造成致命错误:boost/fusion/iterator/equal_to.hpp 没有这样的文件或目录
- Boost.Spirit.Qi 语法,用于 Boost.Fusion 自适应结构中的默认值
- 可变参数宏:重用可变参数(Boost.Fusion)
- Boost.Fusion define struct array member
- 传递向量或论点以bubost :: process(boost :: fusion)
- Casting boost::fusion::vector
- 需要使用 boost::fusion::filter_if 元函数的示例
- 如何在运行时填充Boost :: Fusion ::向量
- boost::spirit解析器和boost::fusion支持哪些容器类型
- 传入一个函数,该函数递增 boost::fusion:vector 的元素
- Boost.Hana中是否有类似Boost.Fusion的View概念
- C++使用boost-fusion-adapt_struct迭代到嵌套结构字段中
- 如何实例化没有默认构造函数的类型的 boost::fusion::vector 成员变量
- boost::fusion::zip function vs boost::fusion::zip_view
- 如何在运行时在boost::fusion::vector中查找元素
- 编译时与使用boost::fusion的运行时
- 在使用 boost::fusion 迭代时将漂亮的打印机绑定到 boost::p hoenix actor
- 使用 boost::fusion 进行迭代时指向类成员的指针
- Boost Fusion invoke and SFINAE