Вопрос

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..

Это было полезно?

Решение

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;

Другие советы

I would also go with kerrek's solution, but other than that, the best thing I could come up with was this.

The problem is that InnerCont is declared as template type in the base template, so you can't specialize it for raw pointer anymore. So you could create a dummy template representing a pointer and use that.

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;

You can't really do what you're doing already actually. Not in a standard way. The C++ containers don't take the same template parameters.

Do something like so:

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;
};

Then you can create different container generators like so:

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

Or your pointer one:

template < typename T, typename Ignored >
struct pointer_gen { typedef T* type; };
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top