/*重构于 16/6/6 */
#include <iostream>
#include <vector>
#include <new>
#include <stdexcept>
#include <utility>
template<typename T>
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<typename Ty>
myAllocate(const myAllocate<Ty>& other)noexcept;
myAllocate(const myAllocate<T>& other)noexcept;
~myAllocate() = default;
myAllocate<T>& operator=(const myAllocate<T>& other)noexcept;
template<typename Ty>
myAllocate<T>& operator=(const myAllocate<Ty>& other)noexcept;
T* allocate(const std::size_t& size);
template<typename Ty>
void constructor(T* ptr, Ty&& value);
template<typename Type, typename ...Types>
void constructor(T* ptr, Type&& value, Types&&... args);
void deallocate(T* ptr, const std::size_t& size);
template<typename Ty>
void destroy(Ty* ptr);
T* address(T& value);
const T* address(const T& value)const;
};
template<typename T>
template<typename Ty>
myAllocate<T>::myAllocate(const myAllocate<Ty>& other)noexcept
{
//
}
template<typename T>
myAllocate<T>::myAllocate(const myAllocate<T>& other)noexcept
{
//
}
template<typename T>
template<typename Ty>
myAllocate<T>& myAllocate<T>::operator=(const myAllocate<Ty>& other)noexcept
{
//
}
template<typename T>
myAllocate<T>& myAllocate<T>::operator=(const myAllocate<T>& other)noexcept
{
}
template<typename T>
T* myAllocate<T>::allocate(const std::size_t& size)
{
if (size == 0) {
return nullptr;
}
else {
return new T[size];
}
}
template<typename T>
template<typename Ty>
void myAllocate<T>::constructor(T* ptr, Ty&& value)
{
if (ptr == nullptr) {
throw std::bad_alloc{};
} else {
new(ptr) T(std::forward<Ty>(value));
}
}
template<typename T>
template<typename Type, typename ...Types> //注意这里. meta.
void myAllocate<T>::constructor(T* ptr, Type&& value, Types&&... args)
{
this->constructor(ptr++, value);
this->constructor(ptr, std::forward<Types>(args)...);
}
template<typename T>
void myAllocate<T>::deallocate(T* ptr, const std::size_t& size)
{
if (ptr == nullptr) {
return;
}
if (size == 1) {
delete ptr;
}
else {
delete[] ptr;
}
}
template<typename T>
template<typename Ty>
void myAllocate<T>::destroy(Ty* ptr)
{
if (ptr = nullptr) {
return;
}
ptr->~Ty();
}
template<typename T>
T* myAllocate<T>::address(T& value)
{
return &value;
}
template<typename T>
const T* myAllocate<T>::address(const T& value)const
{
return &value;
}
int main()
{
return 0;
}