模板模板参数部分特化c ++

时间:2015-06-13 23:13:16

标签: c++ templates

考虑代码:

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

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

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 ) {...}

我在网上看过,发现了类似的问题,但我找不到办法 仅指定模板模板的部分模板参数列表。 有办法吗?

编辑:唯一的问题是,在给出模板类时,部分特化是使其表现为具有其余参数的模板。 这是尝试将C3200: 'std::map<int,V,Comp,int>' : invalid template argument for template parameter 'Container', expected a class parameter.视为仅有2个参数的模板(可以真正用作map<*,Known1,Known2,*>的模板模板参数)。

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

1 个答案:

答案 0 :(得分:3)

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

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

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

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

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

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

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

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