使用 boost::fusion::fold 将 boost::fusion::set 转换为 boost::fusion::map
我有一个融合集,想将其转换为融合图。
#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<>。 谢谢!
I have a fusion set and would like to convert it into a fusion map.
#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;
}
This code compiles. But it couldn't print out the results of my_map. my_set is constructed successfully. my_map is simply converted from a my_set with each element's original type as the key and its instance object from default constructor as the value. I am wondering if my_map is created successfully from fusion::fold. my_set is able to query by calling fusion::at_key<> on it, but my_map doesn't work for at_key<>.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想不出一种简洁的方法来创建
fusion::map
fusion::set
与fold
。所以这个答案不会直接解决你的问题。
我发布此内容是为了给您一些提示。
怎么样
准备类型列表(如下面的
my_types
),以及从该列表创建
fusion::set
和fusion::map
而不是直接从
fusion::set
创建fusion::map
?例如:
这是对 ideone 的测试。
顺便说一句,如果您的
fusion::map
中的 key-type 和 value-type 始终相同,
fusion::set
将满足该目的。I couldn't think of a concise way to create
fusion::map
fromfusion::set
withfold
.So this answer won't resolve your question directly.
I post this in case this would give you some hint.
How about
preparing a type list(as
my_types
in the following), andcreating
fusion::set
andfusion::map
from that listinstead of creating
fusion::map
fromfusion::set
directly?For example:
Here is a test on ideone.
Incidentally, if key-type and value-type in your
fusion::map
are alwaysidentical,
fusion::set
will meet that purpose.