使用mpl :: transform生成一个新序列

时间:2012-10-29 11:44:50

标签: c++ boost-mpl

我正在尝试使用内部格式的fusion::map生成一些将索引映射到类型的通用配置处理代码,如下所示:

fusion::pair< mpl::int_< VALUE >, type >;

为了简化地图的生成,我有以下代码:

#include <boost/fusion/sequence.hpp>
#include <boost/fusion/container.hpp>
#include <boost/fusion/algorithm.hpp>

#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/map.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/placeholders.hpp>

#include <typeinfo>
#include <string>
#include <iostream>

/// create a fusion map of all parameter data
namespace fusion = boost::fusion;
namespace mpl = boost::mpl;

template < unsigned Idx, typename T >
struct config_param
{
    typedef mpl::int_< Idx > index_value;
    typedef T value_type;
};

template< class T >
struct to_fusion_pair
{
    typedef fusion::pair< typename T::index_value
                        , typename T::value_type > type;
};

typedef mpl::vector< config_param< 10, unsigned >
                   , config_param< 20, std::string >
                   , config_param< 30, bool >
                   , config_param< 40, long >
                   , config_param< 50, std::string >
                   > MySequence;

typedef mpl::transform< MySequence, to_fusion_pair< mpl::_1 > >::type ConvertedSeq;

这会编译并生成预期的结果,不幸的是,如果我尝试将调用包装为transform,我会收到大量错误。即:

template< class Seq >
struct to_vector_of_fusion_pair
{
    typedef mpl::transform< Seq, to_fusion_pair< mpl::_1 > >::type type;
};

typedef to_vector_of_fusion_pair<MySequence>::type ConvertedSeq2;

编译器错误如下:

fusion-db.cpp:40: error: type boost::mpl::transform<Seq, to_fusion_pair<mpl_::arg<1> >, mpl_::na, mpl_::na> is not derived from type to_vector_of_fusion_pair<Seq>â
fusion-db.cpp:40: error: expected ; before type

我的to_vector_of_fusion_pair结构有什么问题导致它与mpl::transform不兼容?

1 个答案:

答案 0 :(得分:2)

template< class Seq >
struct to_vector_of_fusion_pair
{
    typedef typename mpl::transform< Seq, to_fusion_pair< mpl::_1 > >::type type;
};

阅读本文以获取解释Where and why do I have to put the "template" and "typename" keywords?