简体   繁体   中英

Passing unspecialized template as a template parameter

Can i do something like

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;
}

So template is passed to the class unspecialiazed and instantiated later.

Yes, here's a minimal compileable example:

#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;
}

Yes, it's called a "template-template parameter", and the syntax is

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM