Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is libstdc++ support for std::unordered_map incomplete?

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.

like image 316
TemplateRex Avatar asked Sep 20 '13 09:09

TemplateRex


1 Answers

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.

like image 69
Jesse Good Avatar answered Sep 29 '22 03:09

Jesse Good