Ken
Ken

Reputation: 719

Inherit container for each type in a boost mpl list

I have a class that I want to inherit from a container for each of the classes in a boost::mpl::vector. in other words, something like this:

template <typename types_vector>
class A : inherit from std::vector<type> for each type in types_vector {

};

for example, if I have this vector:

typedef boost::mpl::vector<bool, int, double> types_vector_;

then A<types_vector_> would expand to:

class A : public std::vector<bool>, public std::vector<int>, public std::vector<double> {

};

How can I do this without using C++11 features (rest of the code isn't ready for that yet)? I think that using boost MPL is the way to go, although if there is alternative other than C++11, I can consider it.

Upvotes: 2

Views: 438

Answers (1)

ForEveR
ForEveR

Reputation: 55887

I think something like this can help you.

#include <boost/mpl/vector.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/mpl/size.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/greater_equal.hpp>
#include <vector>
#include <iostream>

namespace mpl = boost::mpl;

template<typename T,
typename = void>
struct Some
{
   typedef std::vector<T> type;
};

template<typename T>
struct Some<T, 
typename boost::enable_if_c
   <
    mpl::and_
    <
     mpl::is_sequence<T>,
     mpl::greater_equal
     <
      mpl::size<T>, 
      mpl::int_<2>
     >
    >::type::value
   >::type> :
   public Some<typename mpl::front<T>::type>::type,
   public Some<typename mpl::pop_front<T>::type>
{
};

template<typename T>
struct Some<T,
typename boost::enable_if_c
   <
    mpl::and_
    <
     mpl::is_sequence<T>,
     mpl::equal_to
     <
      mpl::size<T>,
      mpl::int_<1>
     > 
    >::type::value
   >::type> :
public Some<typename mpl::front<T>::type>::type
{
};

template<typename T>
struct Some<T,
typename boost::enable_if_c
   <
    mpl::and_
    <
     mpl::is_sequence<T>,
     mpl::equal_to
     <
      mpl::size<T>,
      mpl::int_<0>
     >
    >::type::value
   >::type>
{
};


int main()
{
   typedef mpl::vector<int, double> vect_t;
   typedef Some<vect_t> vector;
   vector vect;
   vect.std::vector<int>::push_back(1);
   vect.std::vector<double>::push_back(2);
   std::cout << "int: " << vect.std::vector<int>::at(0) << std::endl;
   std::cout << "double: " << vect.std::vector<double>::at(0) << std::endl;
}

http://liveworkspace.org/code/ec56ebd25b821c9c48a456477f0d42c9

Upvotes: 3

Related Questions