Search code examples
c++templatespartial-specializationtemplate-templates

Partial template template specialization


have this code:

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;

But need to use T* instead of std::list<T> as InnerCont in some cases - like this:

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

Is it possible to use partial specialization of 'template template' parameter for this case?
Or how to archive it with minimum headache..


Solution

  • It's often easier to template simply on the type. You can't really capture every situation with template templates - what if someone wants to use a container with six template parameters? So try something like this:

    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;