【发布时间】:2017-11-01 19:16:32
【问题描述】:
假设有两个类:
struct A
{
using Key = int;
using Value = char;
};
struct B
{
using Key = char;
using Value = float;
};
我想使用他们的成员类型来定义融合地图:
typedef boost::fusion::map
<
boost::fusion::pair< int , char > ,
boost::fusion::pair< char , float >
> desired_type;
所以我决定使用 MPL fold 来获取类型:
typedef boost::fusion::result_of::as_map< boost::mpl::fold
<
boost::mpl::vector< A , B > ,
boost::fusion::map< > ,
boost::fusion::result_of::push_back
<
boost::mpl::_1 ,
boost::fusion::result_of::make_pair
<
boost::mpl::_2 ::Key , boost::mpl::_2 ::Value
>
>
>::type >::type map_type;
但这当然行不通,因为boost::mpl::_N 确实是返回第 N 个参数的元函数。
所以,我定义了两个辅助元函数:
template< class T >
struct GetInnerKey
{
typedef typename T::Key type;
};
template< class T >
struct GetInnerValue
{
typedef typename T::Value type;
};
并正确定义折叠:
typedef boost::fusion::result_of::as_map< boost::mpl::fold
<
boost::mpl::vector< A , B > ,
boost::fusion::map< > ,
boost::fusion::result_of::push_back
<
boost::mpl::_1 ,
boost::fusion::result_of::make_pair
<
GetInnerKey< boost::mpl::_2 > , GetInnerValue< boost::mpl::_2 >
>
>
>::type >::type map_type;
我的问题是:
有没有办法使用 MPL 或 Fusion 中已经定义的东西来摆脱
GetInnerKey< >和GetInnerValue< >?有没有办法避免使用
boost::fusion::result_of::as_map< >?这是实现我的意图的正确方法吗?
【问题讨论】:
标签: c++ metaprogramming boost-mpl boost-fusion