2

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.

akashihi
  • 917
  • 2
  • 9
  • 19

2 Answers2

4

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;
}
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
1

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;
}
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90