3 ms·
To the point of complexity, the allocation makes the container unfit for over-aligned types. But honestly I don't even know if STL containers are over-alignment
by Bigpet 5y ago
To the point of complexity, the allocation makes the container unfit for over-aligned types. But honestly I don't even know if STL containers are over-alignment aware.
So instead of
(T*)malloc(sizeof(T) * capacity_);
it should be something like this if you want to deal with over-aligned types.
(T*) operator new(sizeof(T)*capacity_, std::align_val_t{alignof(T)});
Also, in the reallocation function the elements are move-assigned to uninitialized memory. They should be move-constructed with a placement-new. And of course calling the destructors of the moved-from objects is still required.
It's of course a little telling that the compiler will not complain about these errors and you have to use and know to use tools like address sanitizer, memory sanatizer and valgrind to let a machine help you find them.