问题描述
相关于在CodeReview上的,我试图使用 std :: unordered_map
使用自定义分配器,但显然这不适用于gcc / clang和libstdc ++。该错误可以通过使用 std :: allocator
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&)
}
。
问:是的libstdc ++构建支持的std :: unordered_map
用一个分配器作为参数不全
Question: is libstdc++ support for constructing std::unordered_map
with a single allocator as argument incomplete?
UPDATE :进一步检查表明,对于除 std :: vector
之外的几乎所有容器, libstdc ++中的分配器直接访问typedef和成员函数,而不是通过 std :: allocator_traits
。这适用于 std :: allocator
,但对所有自定义分配器失败,除非他们直接添加这些成员和typedef。
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.
推荐答案
在最新的在2013-08-01生成,它在第178行:
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)
{ }
不过在文档的它没有,这是我的本地一样。至于g ++ 4.8涉及它的未实现。
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.
。它是日期为2013-04-22,这是一个稍后的4.8版本。
Found the link to the patch. It was dated 2013-04-22, which was a little after the release of 4.8.
这篇关于是libstdc ++支持std :: unordered_map不完整吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!