将非专业化模板作为模板参数传递

时间:2011-04-21 14:18:18

标签: c++ templates stl metaprogramming

我能做点什么吗

template<class Key, class Data, class Compare = less<Key>, template<typename T> class Allocator<T> = allocator<T> >
    class mymap {
        typedef map<Key,Data,Compare,Allocator<pair<const Key, Data> > > storageMap; 
        typedef vector<Data,Allocator<Data> > storageVector;
}

因此模板将被传递给未经过特殊化的类,并在以后实例化。

2 个答案:

答案 0 :(得分:4)

是的,这是一个最小的可编译示例:

#include <map>
#include <vector>
using namespace std;

template <
    class Key,
    class Data,
    class Compare = less<Key>,
    template <typename T> class Allocator = allocator
>
class mymap
{
public:
    typedef map<Key,Data,Compare,Allocator<pair<const Key, Data> > > storageMap; 
    typedef vector<Data,Allocator<Data> > storageVector;
};

int main()
{
    mymap<int,long>::storageMap m;
    mymap<int,long>::storageVector v;
    return 0;
}

答案 1 :(得分:1)

是的,它被称为“模板模板参数”,语法为

template <class Key, class Data, class Compare = less<Key>,
          template <typename T> class Allocator = allocator >
class mymap {
    typedef map<Key,Data,Compare,Allocator<pair<const Key, Data> > > storageMap; 
    typedef vector<Data,Allocator<Data> > storageVector;
}