在 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/