【发布时间】:2018-08-07 06:26:41
【问题描述】:
我有一个这样的boost::fusion::map:
struct bar {};
struct baz {};
template<typename... Args>
struct foo
{
foo(bar &, baz &) {}
};
template<typename... T>
using MapEntry = boost::fusion::pair<boost::mpl::set<T...>, foo<T...>>;
using Map = boost::fusion::map<
MapEntry<int, bool, double>,
MapEntry<std::string>,
MapEntry<int64_t, uint32_t>,
MapEntry<char, uint64_t>
// ...
>;
然后需要初始化Map 的实例,将bar_ 和baz_ 传递给所有foo 构造函数。
目前,我有:
int main()
{
bar bar_;
baz baz_;
Map map_(
boost::fusion::make_pair<boost::mpl::set<int, bool, double>>(foo<int, bool, double>(bar_, baz_)),
boost::fusion::make_pair<boost::mpl::set<std::string>>(foo<std::string>(bar_, baz_)),
boost::fusion::make_pair<boost::mpl::set<int64_t, uint32_t>>(foo<int64_t, uint32_t>(bar_, baz_)),
boost::fusion::make_pair<boost::mpl::set<char, uint64_t>>(foo<char, uint64_t>(bar_, baz_))
// ...
);
return 0;
}
但这是非常多余的。是否有可能使这段代码更干净?
【问题讨论】:
-
为什么你认为这是不可能的?你尝试了什么?你在哪里卡住了?
-
目前我只找到
Map map_( boost::fusion::make_pair<boost::mpl::set<int, bool, double>>(foo<int, bool, double>(bar_, baz_)), boost::fusion::make_pair<boost::mpl::set<std::string>>(foo<std::string>(bar_, baz_)), boost::fusion::make_pair<boost::mpl::set<int64_t, uint32_t>>(foo<int64_t, uint32_t>(bar_, baz_)), boost::fusion::make_pair<boost::mpl::set<char, uint64_t>>(foo<char, uint64_t>(bar_, baz_)) // ... );,但它太多余的解决方案