2012-08-24 3 views
2

boost :: mpl :: vector의 각 클래스에 대한 컨테이너에서 상속 할 클래스가 있습니다. 즉,이 같은 :boost mpl 목록의 각 유형별 컨테이너 상속

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

}; 

,이 벡터가있는 경우 : 내가하지 않고이 작업을 수행 할 수있는 방법

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

}; 

:

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

다음 A<types_vector_>가 확장 것을 C++ 11 기능을 사용하여 (나머지 코드는 아직 준비되지 않았습니다.) 부스트 MPL을 사용하는 것은 갈 길이라고 생각 합니다만, C++ 11 이외의 대안이 있다면 그것을 고려할 수도 있습니다.

+0

흠 ... 당신은 로키를 볼 수 있습니다 ... http://loki-lib.sourceforge.net/ – ForEveR

답변

3

나는 이런 식으로 생각합니다.

#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

관련 문제