加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Boost Fusion / MPL:将类型从序列转换为等效any_range的序

发布时间:2020-12-16 09:33:44 所属栏目:百科 来源:网络整理
导读:我想使用Boost的any_range来处理多个异构数据范围.我的数据范围的类型称为Fusion矢量,例如: typedef vectordouble,int,char TypeSequence 鉴于这样的类型,我想编写一个模板来派生另一个类型: vectorAnyRangedouble::value,AnyRangeint::value,AnyRangechar
我想使用Boost的any_range来处理多个异构数据范围.我的数据范围的类型称为Fusion矢量,例如:

typedef vector<double,int,char> TypeSequence

鉴于这样的类型,我想编写一个模板来派生另一个类型:

vector<AnyRange<double>::value,AnyRange<int>::value,AnyRange<char>::value>

其中AnyRange定义为:

using namespace boost;
template <typename T>
struct AnyRange
{
    typedef typename any_range<typename T,forward_pass_traversal_tag,std::ptrdiff_t> value;
};

我尝试过但都失败了. Fusion甚至可以实现这一点吗? MPL?或许我正在使用any_range走错路.

解决方法

您可以使用 boost::mpl::transform轻松完成此操作,您可以将其与Fusion序列一起使用(只要包含相应的标题以使Fusion序列表现为确认MPL序列):

#include <boost/range/any_range.hpp>

#include <boost/fusion/include/mpl.hpp> // Required to adapt Fusion to MPL
#include <boost/fusion/include/vector.hpp>

#include <boost/mpl/transform.hpp>


template < typename T >
struct EmbedInAnyRange
{
    typedef boost::any_range< // no need for typename here
        T,// no need for typename here
        forward_pass_traversal_tag,// not sure what this parameter is,I leave int...
        std::ptrdiff_t
    > type;
};

int main()
{
    typedef boost::fusion::vector< double,char > Tuple;

    typedef boost::mpl::transform<
        Tuple,EmbedInAnyRange< boost::mpl::_ >
    >::type AnyRangeTuple;

    AnyRangeTuple myTuple( 
        std::vector< double >(),std::list< int >(),std::vector< char >() );
}

如果需要,可以将转换放入自己的元函数中:

template < typename Seq >
struct EmbedAllInAnyRange
{
    typedef typename boost::mpl::transform< // typename needed
        Seq,EmbedInAnyRange< boost::mpl::_ >
    >::type type;
};

...

typedef EmbedAllInRange< Tuple >::type AnyRangeTuple;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读