gpt4 book ai didi

c++ - boost fusion/MPL : convert type from sequence to sequence of equivalent any_range's

转载 作者:太空狗 更新时间:2023-10-29 20:17:59 25 4
gpt4 key购买 nike

我想使用 Boost 的 any_range 来处理多个异构数据范围。我的数据范围类型称为 fusion vector ,例如:

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, int, std::ptrdiff_t> value;
};

我尝试过但失败了。 Fusion甚至可以做到这一点吗? super 联赛?或者,也许我使用 any_range 走错了路。

最佳答案

您可以使用 boost::mpl::transform 轻松完成此操作,您可以将其与 Fusion 序列一起使用(只要包含适当的 header 以使 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,
int, // not sure what this parameter is, I leave int...
std::ptrdiff_t
> type;
};

int main()
{
typedef boost::fusion::vector< double, int, 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;

关于c++ - boost fusion/MPL : convert type from sequence to sequence of equivalent any_range's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5483975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com