Does this program have any problems with exception handling when copying elements?

I tried to use CPP"s built-in array as the container at the bottom of the loop queue
there is a piece of code to reallocate memory
I simplified the code, one of which is as follows

template <typename T, typename Allocator>
inline void Queue<T, Allocator, T *>::reallocate(size_t size) {
    auto new_container {Allocator::operator new (sizeof(T) * size)};
    auto cursor {this->first};
    for(difference_type i {0}; cursor not_eq this->last;) {
        try {
            new (new_container + iPP) T(move(*cursorPP));
        }catch(...) {
            try {
                Allocator::destroy(new_container, new_container + i);
            }catch(...) {
                std::terminate();
            }
            Allocator::operator delete (new_container);
            throw;
        }
    }
    //...
}
Nov.04,2021

mention three problems I found:

    After the
  1. placement new is constructed, the pointer returned by this expression is not used to access the UB. At present, however, there is generally nothing wrong with the compiler.
  2. only basic guarantee. Once an exception is thrown, the container class will lose data.
  3. When
  4. Allocator::destroy, the object being constructed by the current loop cannot be destructed (because it is not finished), otherwise UB. It feels like your writing will destruct the object being constructed in the current loop.

about the second point:
new (new_container + iPP) T (move (* cursorPP)); moves objects from cursor to new_container. If an exception is thrown after moving a part of the object, the part that has been moved will be destructed according to the code you write. In this way, if the user tries to recover from the exception and the container class is not destructed, some of the data will be lost.

on the third point:
when constructing, the constructor of a data member of T throws an exception, causing T to be only partially constructed. At this point, the complete destructor T will UB.

Menu