Boost MPL模板列表

6
我希望能够将一组类模板 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;

我该如何使它工作?


在声明 TypeList 时,您是否遇到任何错误? - iammilind
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> > ));

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>>

在TypeList中


不,我想要完全相反的:mpl::list<T1<Test>, T2<Test>, ...> - Alex B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接