After learning that std::vector is unimplementable in pure C++, I was wondering if it is possible to write a dynamic array without evoking UB. We can't do pointer arithmetic without an array, which implies we cannot have a dynamic buffer with partially initizalized memory and treat it as an array; so std::vector must rely on the implementation defining some behavior where it would otherwise be UB.
Dynamic arrays are pretty ubiquitious data structures, and generally simple. The seemed impossibility of being able to implement this conformantly makes C++ seem like a not-so-general-purpose system language, IMO.
As so, my questions are:
- How one can write a dynamic array (a mundane one, not necessarily a Container) in C++ (without using std::vector) that conforms to the standard?
- How can such implementation be made space-time efficient (preferably without UB or implementation specific behavior)?
N.B.: dynamic array is used here to denote a linear data structure that can grow/shrink "in place", like a std::vector or, similarly, a C buffer (m)alloced in the heap.