博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL: 自定义Allocator.
阅读量:6854 次
发布时间:2019-06-26

本文共 2224 字,大约阅读时间需要 7 分钟。

hot3.png

/*重构于 16/6/6 */#include 
#include
#include
#include
#include
template
class myAllocate {public: using value_type = T; using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = const T&; using size_type = std::size_t; using difference_type = std::ptrdiff_t; myAllocate() = default; template
myAllocate(const myAllocate
& other)noexcept; myAllocate(const myAllocate
& other)noexcept; ~myAllocate() = default; myAllocate
& operator=(const myAllocate
& other)noexcept; template
myAllocate
& operator=(const myAllocate
& other)noexcept; T* allocate(const std::size_t& size); template
void constructor(T* ptr, Ty&& value); template
void constructor(T* ptr, Type&& value, Types&&... args); void deallocate(T* ptr, const std::size_t& size); template
void destroy(Ty* ptr); T* address(T& value); const T* address(const T& value)const;};template
template
myAllocate
::myAllocate(const myAllocate
& other)noexcept{ //}template
myAllocate
::myAllocate(const myAllocate
& other)noexcept{ //}template
template
myAllocate
& myAllocate
::operator=(const myAllocate
& other)noexcept{ //}template
myAllocate
& myAllocate
::operator=(const myAllocate
& other)noexcept{}template
T* myAllocate
::allocate(const std::size_t& size){ if (size == 0) { return nullptr; } else { return new T[size]; }}template
template
void myAllocate
::constructor(T* ptr, Ty&& value){ if (ptr == nullptr) { throw std::bad_alloc{}; } else { new(ptr) T(std::forward
(value)); }}template
template
//注意这里. meta.void myAllocate
::constructor(T* ptr, Type&& value, Types&&... args){ this->constructor(ptr++, value); this->constructor(ptr, std::forward
(args)...);}template
void myAllocate
::deallocate(T* ptr, const std::size_t& size){ if (ptr == nullptr) { return; } if (size == 1) { delete ptr; } else { delete[] ptr; }}template
template
void myAllocate
::destroy(Ty* ptr){ if (ptr = nullptr) { return; } ptr->~Ty();}template
T* myAllocate
::address(T& value){ return &value;}template
const T* myAllocate
::address(const T& value)const{ return &value;}int main(){ return 0;}

 

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/683724

你可能感兴趣的文章