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

旧街凉风 提交于 2019-12-22 04:39:18

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/18913388/is-libstdc-support-for-stdunordered-map-incomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!