为boost mpl列表中的每种类型继承容器



对于boost::mpl::vector中的每个类,我都有一个要从容器继承的类。换句话说,类似这样的东西:

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

例如,如果我有这个向量:

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

则CCD_ 1将扩展为:

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

如果不使用C++11特性(其余代码还没有准备好),我怎么能做到这一点?我认为使用boost MPL是可行的,尽管如果有C++11以外的替代方案,我可以考虑。

我认为这样的东西可以帮助你。

#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

最新更新