Always? No, of course not.
Consider the following example, which uses std::vector for simplicity (suppose that uptr is a smart pointer that acts as a whole like std::unique_ptr ):
std::vector<uptr<T>> vec; vec.insert(vec.begin(), new T());
This is safe for exceptions. A temporary uptr is created to go to insert , which moves to the vector. If the redistribution of the vector fails, the highlighted T belongs to the smart pointer, which correctly deletes it.
Compare with:
std::vector<std::uptr<T>> vec; vec.emplace(vec.begin(), new T());
emplace not allowed to create a temporary object. uptr will be created once, in place in the vector. If the redistribution is not performed, there is no place to create in place, and no smart pointer will be initialized. T will leak out.
Of course, the best alternative is:
std::vector<std::unique_ptr<T>> vec; vec.insert(vec.begin(), make_unique<T>());
which uses a standard smart pointer and makes creating a smart pointer explicit.
source share