#ifndef ARRAY_HPP #define ARRAY_HPP #include #include #include #include #include template class Array { public: Array() : size(0), data(nullptr) { } Array(std::size_t size) : size(size), data(size > 0? static_cast(operator new(sizeof(T) * size)) : nullptr) { for (std::size_t index = 0; index < size; ++index) { new (data + index) T(); } } Array(std::size_t size, const T& t) : size(size), data(size > 0? static_cast(operator new(sizeof(T) * size)) : nullptr) { for (std::size_t index = 0; index < size; ++index) { new (data + index) T(t); } } Array(const Array& other) : size(other.size), data(size > 0? static_cast(operator new(sizeof(T) * size)) : nullptr) { for (std::size_t index = 0; index < size; ++index) { new (data + index) T(other.data[index]); } } friend void swap(Array& a1, Array& a2) { using std::swap; swap(a1.size, a2.size); swap(a1.data, a2.data); } Array(Array&& other) : Array() { swap(*this, other); } ~Array() { for (std::size_t index = 0; index < size; ++index) { data[index].~T(); } operator delete(data); } Array& operator=(Array other) { swap(*this, other); return *this; } std::size_t get_size() const { return size; } T& operator()(std::size_t index) { assert(index < size); return data[index]; } const T& operator()(std::size_t index) const { assert(index < size); return data[index]; } private: std::size_t size; T* data; }; #endif