Template template parameter partial specialization c++

时间:2021-11-24 17:05:38

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 the Container is a map where the Value and Comparator are known, and create a definition of Foo in this case (no specialization of Foo). I tried this:

现在我想专门研究A,在这种情况下,Container是一个已知Value和Comparator的地图,并在这种情况下创建Foo的定义(没有Foo的专业化)。我试过这个:

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 get the compiling error:C3200: 'std::map<int,V,Comp,int>' : invalid template argument for template parameter 'Container', expected a class parameter.

但是我得到了编译错误:C3200:'std :: map ':模板参数'Container'的模板参数无效,需要一个类参数。 ,v,comp,int>

I have looked online and only found similar issues, but I couldn't find a way to specify only partial template parameter list of a template template. Is there a way to do it?

我已经在线查找并且只发现了类似的问题,但我找不到一种方法来仅指定模板模板的部分模板参数列表。有办法吗?

EDIT: The only problem is when giving a template class partial specialization is to make it behave as a template with the remaining parameters. Here it's an attempt to think of a map<*,Known1,Known2,*> as a template of only 2 arguments (which can realy be used as the template template parameter of A) .

编辑:唯一的问题是,给模板类部分特化是使其表现为具有其余参数的模板。这里尝试将地图<*,Known1,Known2,*>视为仅有2个参数的模板(可以真正用作A的模板模板参数)。

EDIT 2: The compiler I have to use is GCC 4.1.2 that has no support for template aliasing, which, as I understood, is a solution.

编辑2:我必须使用的编译器是GCC 4.1.2,它不支持模板别名,据我所知,这是一种解决方案。

1 个解决方案

#1


3  

The problem is that A takes a template template parameter that takes two types:

问题是A采用模板模板参数,它有两种类型:

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

But std::map doesn't take two types. It takes four:

但是std :: map不需要两种类型。需要四个:

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

So that's one problem. You will have to generalize your template to the following (also note the missing class keyword):

这是一个问题。您必须将模板概括为以下内容(还要注意缺少的类关键字):

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

But even with that, at most you could completely explicitly specialize a member function, you can't partially specialize it:

但即便如此,最多你可以完全明确地专门化一个成员函数,你不能部分专门化它:

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

#1


3  

The problem is that A takes a template template parameter that takes two types:

问题是A采用模板模板参数,它有两种类型:

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

But std::map doesn't take two types. It takes four:

但是std :: map不需要两种类型。需要四个:

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

So that's one problem. You will have to generalize your template to the following (also note the missing class keyword):

这是一个问题。您必须将模板概括为以下内容(还要注意缺少的类关键字):

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

But even with that, at most you could completely explicitly specialize a member function, you can't partially specialize it:

但即便如此,最多你可以完全明确地专门化一个成员函数,你不能部分专门化它:

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