|
@@ -3,6 +3,7 @@
|
|
|
|
|
|
#include <cassert>
|
|
|
#include <new>
|
|
|
+#include <utility>
|
|
|
|
|
|
template<typename T>
|
|
|
class Vector {
|
|
@@ -88,7 +89,21 @@ public:
|
|
|
}
|
|
|
|
|
|
void resize(int size) {
|
|
|
- resize(size, T());
|
|
|
+ // calling resize(size, T()); would break structures which can be
|
|
|
+ // default constructed but not copied e.g. unique pointers elements will
|
|
|
+ // be equal to size after this call but not the capacity
|
|
|
+ if(size > elements) {
|
|
|
+ // fill until the given size is reached
|
|
|
+ for(int i = elements; i < size; i++) {
|
|
|
+ push_back(T());
|
|
|
+ }
|
|
|
+ } else if(size < elements) {
|
|
|
+ // remove objects until the size matches
|
|
|
+ for(int i = size; i < elements; i++) {
|
|
|
+ data[i].~T();
|
|
|
+ }
|
|
|
+ elements = size;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void push_back(const T& t) {
|