Consider the following:
std::vector<std::unique_ptr<int>> ptrsToInts;
ptrsToInts.emplace_back(new int);
If reallocation occurs in the vector, and that fails (throwing std::bad_alloc
), am I "safe" or will I leak an int
?
C++11 23.3.6.5 [vector.modifiers]/1 says:
If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator of
T
or by anyInputIterator
operation there are no effects.
which seems to indicate that this is a potential problem. That is, if there are "no effects", then no unique_ptr
ever was constructed, and therefore the destructor behavior one would rely on to delete
that pointer would not occur. (Which might indicate that emplace_back
should be banned for containers of unique_ptr
s)
new
entirely in favor ofstd::make_unique
, so that I can know every allocation ends up on a smart pointer from the get-go. – Harrisharrisburg