Question

Related to this question on CodeReview, I tried to use std::unordered_map with a custom allocator but apparently this does not work with gcc/clang and libstdc++. The error can be generated from initializing an empty hash map with a std::allocator

#include <unordered_map>

int main()
{
    typedef std::allocator<std::pair<const int, int>> A;
    typedef std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, A> H;    

    auto h = H{A()}; // ERROR, cannot find constructor H::H(const A&)
}

Live Example.

Question: is libstdc++ support for constructing std::unordered_map with a single allocator as argument incomplete?

UPDATE: further inspection shows that, for almost all containers other than std::vector, uses of allocators in libstdc++ access typedefs and member functions of allocators directly, rather than through std::allocator_traits. This works for std::allocator but fails for all custom allocators, unless they verbosely add those members and typedefs directly.

Was it helpful?

Solution

In latest doxygen docs generated on 2013-08-01, it's there on line 178:

explicit
unordered_map(const allocator_type& __a)
: _M_h(__a)
{ }

However, in the docs for 4.8.1 it's not there, which is the same as my local one. As as far as g++4.8 is concerned its not implemented.

Found the link to the patch. It was dated 2013-04-22, which was a little after the release of 4.8.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top