Custom allocator for unordered_map<int, vector<Type, allocator> >

Dear all,

Now, I am using TBB, and memory_pool_allocator for scalable memory allocation.

I use unordered_map with its value as vector, and the vector uses some custom allocator.
When I emplace some key&value into the unordered_map, compilation error occurs.

I think that this is due to the default constructor problem which is often seen in use of unordered_map (value has no default constructor).
Do you know how to solve such a situation, value type is vector with custom allocator?


(Indeed, they are concurrent version in TBB, like tbb::concurrent_unordered_map<int, tbb::concurrent_vector<type, allocator> >, and allocator is tbb::memory_pool_allocator)

Kind regards
Now, I am using TBB, and memory_pool_allocator for scalable memory allocation.
tbb::memory_pool_allocator does not have a default constructor. This means that in order to create a vector within the map, an allocator must be supplied explicitly. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <unordered_map>
#include <scoped_allocator>

#define TBB_PREVIEW_MEMORY_POOL 1
#include <tbb/memory_pool.h>

using pool = tbb::memory_pool<std::allocator<unsigned char>>;
template <typename T> using pool_allocator = tbb::memory_pool_allocator<T>;

int main()
{
  using row   = std::vector<int, pool_allocator<int>>;
  using table = std::unordered_map<int, row, std::hash<int>, std::equal_to<int>, 
    std::scoped_allocator_adaptor<pool_allocator<std::pair<const int, row>>>>; 

  pool p;
  pool_allocator<row> alloc(p);  

  table x(alloc); 

  x[0]; // ok
}

https://godbolt.org/z/nMErxc
Last edited on
Dear mbozzi

Thank you for your detailed reply.

BTW, if I use not std::unordered_map<int, std::vector<int> >, but
std::unordered_map<std::unordered_map<int, std::vector<int> > >,
how can I resolve this?

I would appreciate it if you could help me.
Topic archived. No new replies allowed.