deallocate(a, 1); // 解分配一个 int 的空间 // string 的默认分配器 std::allocator<std::string> a2; // 同上,但以 a1 的重绑定获取 decltype(a1)::rebind<std::string>::other a2_1; // 同上,但通过 allocator_traits 由类型 a1 的重绑定获取 std::alloca
任何分配器都可以通过std::allocator_traits<A>::rebind_alloc<T>机制将rebind编辑成另一种类型。
{public:usingvalue_type = T;usingsize_type =size_t;template<classU>structrebind{usingother = StlAlloc<U>; };public:StlAlloc() =default; ~StlAlloc() =default;T *allocate(size_type n, std::allocator<void>::const_pointer hint=0){ std::cout << __FUNCTION__ <<" "<< n <<" "<<...
rebind(deprecated in C++17)(removed in C++20)template<classU> structrebind { typedefallocator<U>other; }; is_always_equal(C++11)(deprecated in C++23)(removed in C++26)std::true_type Member functions (constructor) creates a new allocator instance ...
接着看L2,这行定义了一个对象__a2,其对象类型为using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;,这行的意思是重新封装rebind_alloc<_Sp_counted_ptr_inplace> 继续看L3,在这一行中会创建一块内存,这块内存中按照顺序为创建对象、强引用计数、弱引用计数等(也就是说分配一大块...
{ } _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); } __gnu_cxx::__aligned_buffer<_Tp> _M_storage; }; public: using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>; // Alloc parameter is not a reference so doesn't alias anything in __...
那如何从std::allocator得到std::allocator呢?就可以利用__gnu_cxx::__alloc_traits<std::allocator<int>>::rebind<Node>::other得到std::allocator<Node>。 rebind也是C++标准定义的allocator标准接口之一。 总结一下,就是容器内部可以用rebind来获得用户要求的空间分配器,以分配容器内部需要的空间。
rebind_alloc<T>如果存在就是Alloc::rebind<T>::other,否则在此Alloc是Alloc<U, Args>时就是Alloc<T, Args> rebind_traits<T>std::allocator_traits<rebind_alloc<T>> 成员函数 allocate [静态] 用分配器分配未初始化的存储 (公开静态成员函数)
std::string采用std::allocator<char>作为分配器,由_Compressed_pair的EBO得,分配器并不会占用内存空间。该分配作用于std::_Is_simple_alloc_v<std::_Rebind_alloc_t<std::allocator<char>, char>>为true,因此std::string的内存布局可以拆解如下 // std::string同一时间只可能是短字符串或长字符串union_Bxty...
随着C++11和allocator_traits中的void_pointer和const_void_pointer类型别名的出现,这种需求消失了。但是,...