在c ++ 11中创建自己的分配器时,我正在实现以下接口。这适用于矢量但是当试图将其与地图一起使用时,我会在丢失的项目上出错。我认为这是我需要为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;
错误:
包含来自的文件 /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/map:61:0,
来自main.cpp:3:
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:在 'std :: map的实例化, MyAlloc&gt;':
main.cpp:146:14:从这里要求
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:143:58: 错误:'std :: map中没有名为'pointer'的类型, MyAlloc&gt; :: _ Pair_alloc_type {aka class MyAlloc,200 typedef typename _Pair_alloc_type ::指针指针; ^
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:144:58: 错误:'std :: map,MyAlloc&gt; :: _ Pair_alloc_type {aka class>中没有名为'const_pointer'的类型 MyAlloc /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:145:58: 错误:'std :: map,MyAlloc&gt; :: _ Pair_alloc_type {aka class}中没有名为'reference'的类型 MyAlloc,2个typedef typename _Pair_alloc_type ::参考参考; ^
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:146:58: 错误:'std :: map,MyAlloc&gt; :: _ Pair_alloc_type {aka class>中没有名为'const_reference'的类型 MyAlloc 在包含的文件中 /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/map:60:0,
来自main.cpp:3:
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:In 'void std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue的实例化, _Compare,_Alloc&gt; :: _ M_destroy_node(std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,_Alloc&gt; :: _ Link_t /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree .H:1124:23:
'void std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare, _Alloc&gt; :: _ M_erase(std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,_Alloc&gt; :: _ Link_type /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h :671:28:
'std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare, _Alloc&gt; ::〜_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:
从这里要求
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:421:2: 错误:'std :: _ Rb_tree, std :: _ Select1st&gt;,std :: less, MyAlloc,200ul&gt;计算值:
_M_get_Node_allocator()破坏(__ P)。 ^
make:*** [main.o]错误1
答案 0 :(得分:1)
使用MyAllocator中的某些typedef解决了第一个错误:
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
请发布新的编辑输出。
答案 1 :(得分:1)
基本上我的评论建议我需要为以下内容添加typedef和构造并销毁
template <class U>
struct rebind {typedef MyAlloc<U, N> other;};
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
/// Call constructor with many arguments
template<typename U, typename... Args>
void construct(U* p, Args&&... args)
{
// Placement new
::new((void *)p) U(std::forward<Args>(args)...);
}
/// Call destructor
template<typename U>
void destroy(U* p)
{
p->~U();
}