6

我想获取一个类模板列表T 1 , T 2 , ... T N并列出一个 MPL 类列表,其中每个模板都使用相同的参数进行实例化。

boost::mpl::list不能与模板模板参数列表一起使用,只能与常规类型参数一起使用。

所以以下不起作用:

class A { ... };

template<template <class> class T>
struct ApplyParameterA
{
    typedef T<A> Type;
}

typedef boost::mpl::transform<
    boost::mpl::list<
        T1, T2, T3, T4, ...
    >,
    ApplyParameterA<boost::mpl::_1>::Type
> TypeList;

我怎样才能让它工作?

4

2 回答 2

3

你想要这样的东西:

#include <boost/mpl/list.hpp>
#include <boost/mpl/apply_wrap.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/assert.hpp>

using namespace boost::mpl;

template< typename U > class T1 {};
template< typename U > class T2 {};
template< typename U > class T3 {};

class MyClass;

typedef transform< 
      list< T1<_1>, T2<_1>, T3<_1> >
    , apply1<_1,MyClass>
    >::type r;

BOOST_MPL_ASSERT(( equal< r, list<T1<MyClass>,T2<MyClass>,T3<MyClass> > ));
于 2011-05-06T04:23:46.523 回答
0

我想你想要这个:

#include <boost/mpl/list.hpp>
#include <boost/mpl/transform.hpp>

using namespace boost;
using mpl::_1;

template<typename T>
struct Test {};
struct T1 {};
struct T2 {};
struct T3 {};
struct T4 {};

template<template <class> class T>
struct ApplyParameterA
{
    template<typename A>
    struct apply
    {
        typedef T<A> type;
    };
};

typedef mpl::transform<
             mpl::list<T1, T2, T3, T4>,
             mpl::apply1<ApplyParameterA<Test>, _1>
        > TypeList;

这将使

mpl::list<Test<T1>, Test<T2>, Test<T3>, Test<T4>>

在类型列表中

于 2011-05-05T07:55:17.740 回答