Given a fusion sequence X, I would like to create a new fusion sequence Y whose implementation will be based on X. In particular, I would like to create a class template make_fusion_conforming
so that
template <class X>
struct another_fusion_sequence : make_fusion_conforming <X> {};
will make another_fusion_sequence<X>
fusion-conforming, so that I could call
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);
where it1
and it2
would be iterators to the first element of the sequences (which would in fact be the first element of the X sequence). Note that line 2 is also valid since another_fusion_sequence<X>
is already fusion-conforming.
Using the boost extension documentation and the provided triple.cpp demo I came up with what is below. Apologies it is bit long but it is very simple as the implementation of iterator
and make_fusion_conforming
simply delegate to the already fusion-conforming sequence.
#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;
};
};
Unfortunately, trying to take the begin iterator fails...
#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() {}
Can someone help me with this? Thank you
The problem seems to be that Boost.Fusion's extension machinery does not recognize that make_fusion_conforming
is a "sequence façade". The first thing fusion does when working with intrinsic operations (begin, end, value_at, etc...) is check the tag of the sequence in question. It first checks if your type has a fusion_tag
associated type (and takes it as its tag), then checks if it's a MPL sequence and if the checks fail it determines that the sequence has a non_fusion_tag
tag. In order for all your boilerplate to work the tag of sequence1
needs to be sequence_facade_tag
, and you get that by deriving from sequence_facade
but by also deriving from Base
(fusion::vector
in this case that has a tag of vector_tag
) you have made fusion_tag
ambiguous and accordingly tag_of<seq1>::type
turns to be non_fusion_tag
. A possible solution could be adding a using fusion_tag = fusion::sequence_facade_tag;
to make_fusion_conforming
resolving the ambiguity (another could be specializing boost::fusion::traits::tag_of<sequence1<T...>>::type
).