考虑代码:
template<template<typename,typename> class Container>
class A {
template<class U, class Allocator>
void Foo(Container<U, Allocator>*, U);
};
Run Code Online (Sandbox Code Playgroud)
现在我想专门研究A容器是已知值和比较器的映射的情况,并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 ) {...}
Run Code Online (Sandbox Code Playgroud)
但我得到编译错误:C3200: 'std::map<int,V,Comp,int>' : invalid template argument for template parameter 'Container', expected a class parameter.
我在网上查了一下,只发现了类似的问题,但我找不到一种方法来仅指定模板模板的部分模板参数列表。有办法做到吗?
编辑:唯一的问题是当给予模板类部分专业化时,使其表现为具有剩余参数的模板。这里尝试将 a 视为map<*,Known1,Known2,*>只有 2 个参数的模板(实际上可以用作 的模板模板参数A)。
编辑2:我必须使用的编译器是GCC 4.1.2,它不支持模板别名,据我所知,这是一个解决方案。
问题是A采用的模板模板参数采用两种类型:
template<template<typename,typename> Container>
class A {
Run Code Online (Sandbox Code Playgroud)
但std::map不采用两种类型。需要四个:
template<
class Key,
class T,
class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >
> class map;
Run Code Online (Sandbox Code Playgroud)
所以这是一个问题。您必须将模板概括为以下内容(另请注意缺少的class关键字):
template <template <typename...> class Container>
class A { ... };
Run Code Online (Sandbox Code Playgroud)
但即便如此,最多你可以完全显式地特化一个成员函数,但你不能部分地特化它:
template <>
template <>
void A<std::map>::Foo<>(std::map<U, V>*, U) { ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2698 次 |
| 最近记录: |