Simple resizable array In some (lower-level) parts of my codebase I often have the need to allocate resizable array storage for certain objects, that usually are not default-constructible.

I created a simple class using std::aligned_storage and I was wondering if it's safe and if it can be improved.

The user of the class has to keep track of the current capacity.

template&lt;typename T&gt; class ResizableArray
{
    private:
        using TStorage = std::aligned_storage_t&lt;sizeof(T), alignof(T)&gt;;
        TStorage* data{nullptr};

    public:
        ~ResizableArray() { delete[] data; }

        void resize(std::size_t mSizeOld, std::size_t mSizeNew)
        {
            auto newData(new TStorage[mSizeNew]);
            for(auto i(0u); i &lt; mSizeOld; ++i) newData[i] = std::move(data[i]);

            delete[] data;
            data = newData;
        }

    auto&amp; operator[](std::size_t mIdx) noexcept 
    { return reinterpret_cast&lt;T&amp;&gt;(data[mIdx]); }

    const auto&amp; operator[](std::size_t mIdx) const noexcept 
    { return reinterpret_cast&lt;const T&amp;&gt;(data[mIdx]); }
};


Example usage:

int main()
{
    ResizableArray&lt;Thing&gt; a;

    // Resize array capacity from 0 to 10
    a.resize(0, 10);

    for(auto i(0u); i &lt; 10; ++i)
        new (&amp;a[i]) Thing{/* something */};

    return 0;
}
I see a couple of problems:


If you ever shrink the array, the loop for copying the elements will overrun the bounds of newdata because the index runs from 0 to mSizeOld.
You don't store the size of the array in the class, so code like this will lose data:

ResizeableArray&lt;Thing&gt; a;
a.resize(0, 10);
// Populate the array
a.resize(0, 11);
// Now a is empty again because resize() uses the first argument.


This might be intentional &mdash; to allow you to start over with a fresh set of data &amp;mdash but if so, resize() should be commented to say that.