3

any_rangeBoost を使用して、複数の異種データ範囲を処理したいと考えています。データ範囲のタイプは、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, int, std::ptrdiff_t> value;
};

私は試して失敗しました。これはFusionでも可能ですか?MPL?あるいは、間違った道を進んでいるのかもしれませんany_range

4

1 に答える 1

7

You can do this easily using boost::mpl::transform, which you can use with Fusion sequences (as long as you include the appropriate headers to make Fusion sequences behave as confirming MPL sequences):

#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 >() );
}

If you want, you can put the transformation into its own metafunction:

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

...

typedef EmbedAllInRange< Tuple >::type AnyRangeTuple;
于 2011-03-30T09:30:10.663 に答える