1

이 코드가 있습니다부분 템플릿 템플릿 특수화

template<typename T, template<typename, typename> class OuterCont, template<typename, typename> class InnerCont, class Alloc=std::allocator<T>> 
class ContProxy { 
    OuterCont<T, InnerCont<T, Alloc>> _container; 
}; 
typedef ContProxy<int, std::vector, std::list> IntCont; 

을하지만 어떤 경우에는 InnerContT* 대신 std::list<T>를 사용할 필요가 -이 같은 :

template<typename T, template<typename, typename> class OuterCont, T*, class Alloc=std::allocator<T>> 
class ContProxy { 
    OuterCont<T, T*> _container; 
}; 

이의 부분 특수화를 사용하는 것이 가능 이 경우의 '템플릿 템플릿'매개 변수는 무엇입니까?
또는 방법 최소 두통을 보관하는 방법은 ..

답변

3

그것은 유형에 간단하게 템플릿을하는 것이 더 쉽다. 템플릿 템플릿을 사용하여 모든 상황을 캡처 할 수는 없습니다. 템플릿 매개 변수가 6 개인 컨테이너를 사용하려면 어떻게해야합니까? 따라서이 같은 시도 :

template <typename T, typename C> 
struct ContProxy 
{ 
    typedef C     container_type; 
    typedef typename C::second_type second_type; 

    container_type container_; 
}; 

ContProxy<int, MyContainer<int, std::list<int>> p; 
0

나는 또한 kerrek의 솔루션으로 갈 것,하지만 그 이외의, 내가 가지고 올 수있는 가장 좋은 방법이이었다.

문제는 기본 템플릿에서 InnerCont가 템플릿 유형으로 선언되어 더 이상 원시 포인터로 사용할 수 없다는 것입니다. 그래서 포인터를 나타내는 더미 템플릿을 만들어 사용할 수 있습니다.

template<typename,typename> class PtrInnerCont; //just a dummy template that does nothing 

template<typename T, template<typename, typename> class OuterCont, template<typename, typename> class InnerCont, class Alloc=std::allocator<T>> 
class ContProxy { 
    OuterCont<T, PtrInnerCont<T, Alloc>> _container; 
}; 
typedef ContProxy<int, std::vector, std::list> IntCont; 

template<typename T, template<typename, typename> class OuterCont, class Alloc> 
class ContProxy<T, OuterCont, PtrInnerCont, Alloc> { 
    OuterCont<T, T*> _container; 
}; 

typedef ContProxy<int, std::vector, PtrInnerCont> MyCont; 
0

실제로 이미 실제로하고있는 일을 실제로 할 수는 없습니다. 표준 방식이 아닙니다. C++ 컨테이너는 동일한 템플릿 매개 변수를 사용하지 않습니다.

과 같이 뭔가를 수행

template< typename T, 
      template<typename, typename> class OuterCont, 
      template<typename, typename> class InnerCont, 
      class Alloc=std::allocator<T>> 
class ContProxy { 
    typename OuterCont<T, typename InnerCont<T, Alloc>::type>::type _container; 
}; 

는 그런 다음과 같이 다른 컨테이너 발전기를 만들 수 있습니다

template < typename T, typename A = std::allocator<T> > 
struct vector_gen { typedef std::vector<T,A> type; }; 

또는 하나의 포인터를 :

template < typename T, typename Ignored > 
struct pointer_gen { typedef T* type; }; 
관련 문제