...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <memory> template <typename T, typename Alloc = std::allocator<T>>allocator<T> > class Container { T *UnderlyingStorage; size_t NumElements; void CopyElements(T *From, T *To, size_t Count); public: void reserve(size_t Count) { if (Count > NumElements) { Alloc A; T *P = A.allocate(Count); // Throws on failure try { CopyElements(UnderlyingStorage, P, NumElements); } catch (...) { A.deallocate(P, Count); throw; } UnderlyingStorage = P; } NumElements = Count; } T &operator[](size_t Idx) { return UnderlyingStorage[Idx]; } const T &operator[](size_t Idx) const { return UnderlyingStorage[Idx]; } }; |
...