c++ - libstdc++ 对 std::unordered_map 的支持是否不完整?

标签 c++ c++11 unordered-map allocator libstdc++

this question相关在 CodeReview 上,我尝试将 std::unordered_map 与自定义分配器一起使用,但显然这不适用于 gcc/clang 和 libstdc++。该错误可能是通过使用 std::allocator

初始化空 HashMap 而产生的
#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 .

问题:libstdc++ 是否支持使用单个分配器作为参数构建std::unordered_map

更新:进一步检查表明,对于除 std::vector 之外的几乎所有容器,在 libstdc++ 中使用分配器直接访问类型定义和分配器的成员函数,而不是而不是通过 std::allocator_traits。这对 std::allocator 有效,但对所有自定义分配器都无效,除非它们直接详细地添加这些成员和 typedef。

最佳答案

最新doxygen docs生成于 2013-08-01,它在第 178 行:

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

但是,在 4.8.1 的文档中它不在那里,这和我本地的一样。就 g++4.8 而言,它没有实现。

Found the link to the patch .它的日期是 2013-04-22,比 4.8 发布晚了一点。

关于c++ - libstdc++ 对 std::unordered_map 的支持是否不完整?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18913388/

相关文章:

c++ - 通过 Rcpp 正确注册插件以使用 Eigen

c++ - 使用 lambda 调用的 move 构造函数

c++ - 为什么 `std::unordered_map::erase(key_type const&)` 返回已移除元素的数量?

c++ - 模板化构造函数中的奇怪错误

c++ - 是否需要显式对齐?

c++ - 为派生类 move 构造函数

c++ - STL 结构 : "insert if not present" operation?

C++11 unordered_set with std::owner_less-like hashing

c++ - 监控事件目录事件

debugging - 有没有人获得 lldb 表达式评估以在 Mavericks 上使用 C++11?