我必须提供的 C++11 分配器默认接口(interface)

标签 c++11 memory-management allocator

在 c++11 中创建我自己的分配器时,我正在实现以下接口(interface)。这适用于矢量,但当尝试将其与 map 一起使用时,我会收到缺少项目的错误。我认为这就是我需要为 c++11 实现的全部内容,因为它将在 STL 实现中使用 allocator_traits。我在这里缺少什么?我是否需要为 std::map 的分配器定义更多方法/数据结构?当前尝试编译时,我看到以下错误(见下文)。第 3 行 main.cpp 只是

#include <map>

template <class T> struct MyAllocator {
    typedef T value_type;
    MyAllocator() noexcept;  // only required if used
    MyAllocator(const MyAllocator&) noexcept;  // copies must be equal
    MyAllocator(MyAllocator&&) noexcept;  // not needed if copy ctor is good enough
    template <class U> MyAllocator(const MyAllocator<U>& u) noexcept; 
        // requires: *this == MyAllocator(u)
    value_type* allocate(std::size_t);
    void deallocate(value_type*, std::size_t) noexcept;
};

template <class T, class U> bool
operator==(const MyAllocator<T>&, const MyAllocator<U>&) noexcept;

错误:

In file included from /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/map:61:0,
from main.cpp:3:
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h: In instantiation of ‘class std::map, MyAlloc >’:
main.cpp:146:14: required from here
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:143:58: error: no type named ‘pointer’ in ‘std::map, MyAlloc >::_Pair_alloc_type {aka class MyAlloc, 200 typedef typename _Pair_alloc_type::pointer pointer; ^
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:144:58: error: no type named ‘const_pointer’ in ‘std::map, MyAlloc >::_Pair_alloc_type {aka class MyAlloc /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:145:58: error: no type named ‘reference’ in ‘std::map, MyAlloc >::_Pair_alloc_type {aka class MyAlloc, 2 typedef typename _Pair_alloc_type::reference reference; ^
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:146:58: error: no type named ‘const_reference’ in ‘std::map, MyAlloc >::_Pair_alloc_type {aka class MyAlloc In file included from /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/map:60:0,
from main.cpp:3:
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h: In instantiation of ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_destroy_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_t /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:1124:23:
required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:671:28:
required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = int; _Val = std::pair; _KeyOfValue = std::
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:96:11:
required from here
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:421:2: error: ‘std::_Rb_tree, std::_Select1st >, std::less, MyAlloc, 200ul> >:
_M_get_Node_allocator().destroy(__p); ^
make: *** [main.o] Error 1

最佳答案

第一个错误是使用 MyAllocator 中的一些 typedef 解决的:

typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;

请发布您的新编译输出。

关于我必须提供的 C++11 分配器默认接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26535514/

相关文章:

c++ - 该片段在 Coliru 中编译时带有警告,但在 Ideone 中编译正常。哪一个是正确的?

c++11 - shared_ptr<T> 到 const shared_ptr<const T>&

iphone - 如何制作快速填满内存的iPhone应用程序

c++ - 如何处理指针成员的不同所有权策略?

c++ - 为什么有两个 std::allocator::construct 函数?

c++ - 如何获得 std::map 的真正分配器?

c++ - 如何更好地实现这个自 Action 用域模板对象?

c++ - 为什么 std::array::size 不是静态的?

c++ - 在内存管理中调用有效的构造函数

c++ - 从 std::vector 接管内存