Using unordered_map allocator's not default constructor

442 Views Asked by At

I would like to use a different allocator than the default one in unordered_map.
For my specific allocator, I want to call another constructor than the default one (I want to pass an int to the constructor).
I think the problem is that unordered_map calls the default constructor of my allocator.

So, Eventually I want to do something like that:

Instead of calling this unordered_map constructor:

std::tr1::unordered_map<int, int, std::tr1::hash<int>, std::equal_to<int>, MyAllocatorNs::MyAllocator<std::pair<const int, int> > > unorderedMap;

I want to do something like that:

std::tr1::unordered_map<int, int, std::tr1::hash<int>, std::equal_to<int>, MyAllocatorNs::MyAllocator<std::pair<const int, int> >(3) > unorderedMap;

Is it possible anyhow?

1

There are 1 best solutions below

0
TartanLlama On BEST ANSWER

unordered_map allows you to pass in an instance of the allocator as an argument rather than defaulting to the zero-argument constructor.

typedef MyAllocatorNs::MyAllocator<std::pair<const int, int> > AllocatorType;
unordered_map<int, int, hash<int>, equal_to<int>, AllocatorType > unorderedMap (AllocatorType(3));