使用另一个创建新的增强融合序列
Creating a new boost fusion sequence using another one
给定一个融合序列 X,我想创建一个新的融合序列 Y,其实现将基于 X。特别是,我想创建一个 class 模板 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);
其中 it1
和 it2
将是序列第一个元素的迭代器(实际上是 X 序列的第一个元素)。请注意,第 2 行也是有效的,因为 another_fusion_sequence<X>
已经符合融合。
使用 boost 扩展文档和提供的 triple.cpp 演示,我想出了下面的内容。抱歉,它有点长,但它非常简单,因为 iterator
和 make_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;
};
};
不幸的是,尝试获取开始迭代器失败...
#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() {}
有人可以帮我解决这个问题吗?谢谢
问题似乎是 Boost.Fusion 的扩展机器无法识别 make_fusion_conforming
是 "sequence façade"。当使用内部操作(开始、结束、value_at 等)时,融合所做的第一件事是检查相关序列的标签。它首先检查您的类型是否具有 fusion_tag
关联类型(并将其作为其标签),然后检查它是否是 MPL 序列,如果检查失败,则确定该序列具有 non_fusion_tag
标签。为了让你所有的样板都能工作,sequence1
的标签需要是 sequence_facade_tag
,你可以通过从 sequence_facade
派生但也可以从 Base
派生得到它(fusion::vector
在这种情况下有一个标签 vector_tag
) 你已经使 fusion_tag
模棱两可,因此 tag_of<seq1>::type
变成了 non_fusion_tag
。一个可能的解决方案是在 make_fusion_conforming
中添加一个 using fusion_tag = fusion::sequence_facade_tag;
来解决歧义(另一个可能是专门化 boost::fusion::traits::tag_of<sequence1<T...>>::type
)。
给定一个融合序列 X,我想创建一个新的融合序列 Y,其实现将基于 X。特别是,我想创建一个 class 模板 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);
其中 it1
和 it2
将是序列第一个元素的迭代器(实际上是 X 序列的第一个元素)。请注意,第 2 行也是有效的,因为 another_fusion_sequence<X>
已经符合融合。
使用 boost 扩展文档和提供的 triple.cpp 演示,我想出了下面的内容。抱歉,它有点长,但它非常简单,因为 iterator
和 make_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;
};
};
不幸的是,尝试获取开始迭代器失败...
#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() {}
有人可以帮我解决这个问题吗?谢谢
问题似乎是 Boost.Fusion 的扩展机器无法识别 make_fusion_conforming
是 "sequence façade"。当使用内部操作(开始、结束、value_at 等)时,融合所做的第一件事是检查相关序列的标签。它首先检查您的类型是否具有 fusion_tag
关联类型(并将其作为其标签),然后检查它是否是 MPL 序列,如果检查失败,则确定该序列具有 non_fusion_tag
标签。为了让你所有的样板都能工作,sequence1
的标签需要是 sequence_facade_tag
,你可以通过从 sequence_facade
派生但也可以从 Base
派生得到它(fusion::vector
在这种情况下有一个标签 vector_tag
) 你已经使 fusion_tag
模棱两可,因此 tag_of<seq1>::type
变成了 non_fusion_tag
。一个可能的解决方案是在 make_fusion_conforming
中添加一个 using fusion_tag = fusion::sequence_facade_tag;
来解决歧义(另一个可能是专门化 boost::fusion::traits::tag_of<sequence1<T...>>::type
)。