Implicit memory management with a smart pointer
In C++, the simplest way to change our Vector<T> implementation from one that manually manages memory to one that does so implicitly is through a smart pointer. The idea here is, essentially, to change the type of the elems data member of Vector<T> from T* to std::unique_ptr<T[]>. We will look at this from two angles:
- How does this change impact the naïve version of
Vector<T>? As a reminder, our naïve version from Chapter 12 did not distinguish between objects and raw memory in the underlying storage, and thus only stored objects. This led to a simpler implementation, but also one that needlessly constructed objects on many occasions and was much slower than the more sophisticated implementation for non-trivially constructible types. - How does this change impact the sophisticated version of
Vector<T>that avoided the performance trap of constructing unnecessary objects at the cost of...