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
  • 887
  • 2
  • 8
  • 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
  • 19,702
  • 4
  • 49
  • 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
  • 23,707
  • 11
  • 74
  • 87