Boost MPL list of templates
Asked Answered
M

2

6

I want to take a list of class templates, T1, T2, ... TN and have a list an MPL list of classes, where each template is instantiated with the same parameter.

boost::mpl::list cannot be used with a list of template template parameters, just regular type parameters.

So the following does not work:

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;

How can I make it work?

Molding answered 5/5, 2011 at 6:34 Comment(1)
are you getting any error while declaring TypeList ?Oraliaoralie
A
3

You want something like this:

#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> > ));
Attendance answered 6/5, 2011 at 4:23 Comment(0)
B
0

I think you want this:

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

this will make

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

in TypeList

Boswall answered 5/5, 2011 at 7:55 Comment(1)
No, I want exactly the opposite: mpl::list<T1<Test>, T2<Test>, ...>Molding

© 2022 - 2024 — McMap. All rights reserved.