C ++ partial specialization template template parameter

Consider the code:

template<template<typename,typename> class Container>
class A {
    template<class U, class Allocator>
    void Foo(Container<U, Allocator>*, U);
};

      

Now I want to specialize A

in the case that the Container is a map where the value and comparator are known, and create a definition in that case Foo

(no specialization Foo

). I've tried this:

template<typename V, typename Comp> template<class U, class Allocator>
void A<std::map<typename, V, Comp, typename>>::Foo(map<U, V, Comp, Allocator>*, U ) {...}

      

But I am getting compile error: C3200: 'std::map<int,V,Comp,int>' : invalid template argument for template parameter 'Container', expected a class parameter.

I looked online and only found similar problems, but I couldn't find a way to specify only a partial pattern of the pattern template parameters. Is there a way to do this?

EDIT: The only problem is that providing a partial specialization of a template class is to make it behave like a template with the remaining parameters. Here it is an attempt to think of map<*,Known1,Known2,*>

as a template with only two arguments (which can be used as a template template parameter A

).

EDIT 2: The compiler I have to use is GCC 4.1.2, which does not support template aliases, which I figured is the solution.

+3


source to share


1 answer


The problem is that it A

takes a templated templated parameter that takes two types:

template<template<typename,typename> Container>
class A {

      

But it std::map

doesn't take two types. It takes four:

template<
    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T> >
> class map;

      



So, one problem. You will need to generalize your template to the following (also note the missing keyword class

):

template <template <typename...> class Container>
class A { ... };

      

Even so, at best, you can fully explicitly highlight the member function, you cannot partially specialize it:

template <>
template <>
void A<std::map>::Foo<>(std::map<U, V>*, U) { ... }

      

+3


source







All Articles