C++11 C++;11我必须提供的分配器默认接口
在c++11中创建自己的分配器时,我实现了以下接口。这适用于vector,但当尝试将其用于map时,我会发现缺少项的错误。我认为这就是我需要为c++11实现的全部内容,因为它将在stl实现中使用分配器特性。我错过了什么?我是否需要为std::map的分配器定义更多的方法/数据结构?当前尝试编译时,我看到以下错误(请参见下文)。第3行main.cpp只是C++11 C++;11我必须提供的分配器默认接口,c++11,memory-management,allocator,C++11,Memory Management,Allocator,在c++11中创建自己的分配器时,我实现了以下接口。这适用于vector,但当尝试将其用于map时,我会发现缺少项的错误。我认为这就是我需要为c++11实现的全部内容,因为它将在stl实现中使用分配器特性。我错过了什么?我是否需要为std::map的分配器定义更多的方法/数据结构?当前尝试编译时,我看到以下错误(请参见下文)。第3行main.cpp只是 #include <map> template <class T> struct MyAllocator {
#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;
#包括
模板结构MyAllocator{
类型定义T值_类型;
MyAllocator()noexcept;//仅在使用时需要
MyAllocator(const MyAllocator&)noexcept;//副本必须相等
MyAllocator(MyAllocator&&)noexcept;//如果复制构造函数足够好,则不需要
模板MyAllocator(常量MyAllocator&u)无异常;
//需要:*此==MyAllocator(u)
值类型*分配(标准::大小\u t);
无效解除分配(值类型*,标准::大小)无例外;
};
模板布尔
运算符==(常量MyAllocator&,常量MyAllocator&)无异常;
错误:
在文件中包括从
/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:In “类std::map”的实例化, MyAlloc>“:
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>::_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: 错误:在'std::map,MyAlloc>:::\u Pair\u alloc\u type{aka class]中没有名为'const\u pointer'的类型 MyAlloc /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_-map.h:145:58: 错误:“std::map,MyAlloc>:::\u Pair\u alloc\u type{aka class]中没有名为“reference”的类型 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: 错误:在“std::map,MyAlloc>:::\u Pair\u alloc\u type{aka class]中没有名为“const\u 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::_M_destroy_node(std::_Rb_tree::_Link_t/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:1124:23:
“void std::_Rb_tree::_M_erase(std:_Rb_tree::_Link_type/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:671:28:
“std::_Rb_tree::~_Rb_tree()[带_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::\u Rb\u树, std::_Select1st>,std::less, MyAlloc,200ul>>:
_M_get_Node_allocator().销毁(u p)^
make:**[main.o]错误1
使用MyAllocator中的一些typedef解决了第一个错误:
typedef T&reference;
类型定义常数T和常数U参考;
typedef T*指针;
typedef const T*const_指针;
请发布您的新编译输出。基本上正如我的评论所建议的,我需要为以下内容添加typedef和一个construct and destroy
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();
}
模板
结构重新绑定{typedef MyAlloc other;};
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T*指针;
typedef const T*const_指针;
typedef T&reference;
类型定义常数T和常数U参考;
类型定义T值_类型;
///使用多个参数调用构造函数
模板
无效构造(U*p、参数和…参数)
{
//新位置