Boost MPL模板列表

Boost MPL list of templates

本文关键字:列表 MPL Boost      更新时间:2023-10-16

我想取一个类模板的列表, T1, T2,…TN并有一个类的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;

我怎样才能使它工作?

您想要这样的内容:

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

我想你想要这个:

#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>>
在TypeList