使用另一个

时间:2016-12-02 11:59:38

标签: c++ template-meta-programming boost-fusion

鉴于融合序列X,我想创建一个新的融合序列Y,其实现将基于X.特别是,我想创建一个类模板make_fusion_conforming,以便

template <class X>
struct another_fusion_sequence : make_fusion_conforming <X> {};

会使another_fusion_sequence<X>符合融合,以便我可以调用

auto sequence1 = another_fusion_sequence<X>(); //line 1
auto sequence2 = another_fusion_sequence<another_fusion_sequence<X>>(); // line 2
auto it1 = fusion::begin(sequence1);
auto it2 = fusion::begin(sequence2);

其中it1it2将是序列的第一个元素的迭代器(实际上它将是X序列的第一个元素)。请注意,第2行也有效,因为another_fusion_sequence<X>已经符合融合。

使用boost扩展文档和提供的triple.cpp演示,我想出了以下内容。道歉它有点长,但它很简单,因为iteratormake_fusion_conforming的实现只是委托给已融合的序列。

#include <boost/fusion/iterator/iterator_facade.hpp>
#include <boost/fusion/iterator/value_of.hpp>
#include <boost/fusion/iterator/deref.hpp>
#include <boost/fusion/iterator/next.hpp>
#include <boost/fusion/iterator/prior.hpp>
#include <boost/fusion/iterator/distance.hpp>
#include <boost/fusion/include/begin.hpp>
#include <boost/fusion/include/end.hpp>
#include <boost/fusion/include/size.hpp>
#include <boost/fusion/include/category_of.hpp>
#include <boost/fusion/sequence/sequence_facade.hpp>

namespace fusion = boost::fusion;

// Sequence is the sequence I want to make fusion-conforming
// BaseIter is a fusion-conforming iterator
// the implementation of iterator below is based entirely on BaseIter
template <class Sequence, class BaseIter>
struct iterator
: fusion::iterator_facade<iterator<Sequence, BaseIter>, typename fusion::traits::category_of<BaseIter>::type> {

    using base_iter_type = BaseIter;

    BaseIter m_base_iter;

    iterator(BaseIter base_iter)
    : m_base_iter(base_iter){}

    template <class It>
    struct value_of {

        using type = typename fusion::result_of::value_of<typename It::base_iter_type>::type;
    };

    template <class It>
    struct deref {

        using type = typename fusion::result_of::deref<typename It::base_iter_type>::type;

        static type call(const It& iter) {
            return fusion::deref(iter.m_base_iter);
        }
    };

    template <class It>
    struct next {

        using type = typename fusion::result_of::next<typename It::base_iter_type>::type;

        static type call(const It& iter) {
            return fusion::next(iter.m_base_iter);
        }
    };

    template <class It>
    struct prior {

        using type = typename fusion::result_of::prior<typename It::base_iter_type>::type;

        static type call(const It& iter) {
            return fusion::prior(iter.m_base_iter);
        }
    };

    template <class It1, class It2>
    struct distance {

        using type = typename fusion::result_of::distance<typename It1::base_iter_type, typename It2::base_iter_type>::type;

        static type call(const It1& iter1, const It2& iter2) {
            return fusion::distance(iter1.m_base_iter, iter2.m_base_iter);
        }
    };

};

// Base is the fusion-conforming sequence
// Sequence is the sequence I want to make fusion-conforming
// once again, implementation is based entirely on the Base sequence
template <class Base, class Sequence>
struct make_fusion_conforming
: public Base
, public fusion::sequence_facade<Sequence, typename fusion::traits::category_of<Base>::type> {

    using Base::Base;

    template <class Seq>
    struct begin {

        using type = iterator<Seq, typename fusion::result_of::begin<Base>::type>;

        static type call(Seq& seq) {
            return type(fusion::begin(static_cast<Base&>(seq)));
        }
    };

    template <class Seq>
    struct end {

        using type = iterator<Seq, typename fusion::result_of::end<Base>::type>;

        static type call(Seq& seq) {
            return type(fusion::end(static_cast<Base&>(seq)));
        }
    };

    template <class Seq>
    struct size {
        using type = typename fusion::result_of::size<Base>::type;

        static type call(Seq& seq) {
            return fusion::size(static_cast<Base&>(seq));
        }
    };

    template <class Seq>
    struct empty {

        using type = typename fusion::result_of::empty<Base>::type;

        static type call(Seq& seq) {
            return fusion::empty(static_cast<Base&>(seq));
        }
    };

    template <class Seq, class N>
    struct at {

        using type = typename fusion::result_of::at<Base, N>::type;

        static type call(Seq& seq) {
            return fusion::at<N>(static_cast<Base&>(seq));
        }
    };

    template <class Seq, class N>
    struct value_at {

        using type = typename fusion::result_of::value_at<Base, N>::type;
    };
};

不幸的是,尝试使用begin迭代器失败了......

#include <boost/fusion/include/vector.hpp>

template <class... T>
struct sequence1
: make_fusion_conforming< fusion::vector<T...>, sequence1<T...> > {

};

using seq1 = sequence1<int, float, double>;

// the below fails with:
// No type named 'type' in 'boost::fusion::result_of::begin<sequence1<int, float, double> >'
using test1 = fusion::result_of::begin<seq1>::type;

int main() {}

有人可以帮我吗?谢谢

0 个答案:

没有答案