No, the first scenario is not safe and will leak memory if the vector
throws an exception.
The second scenario will not compile, as a std::unique_ptr<T>
cannot be implicitly converted to T*
. Even if it could, this scenario is arguably worse than the first, because it will add the pointer to your vector, then immediately delete the object being pointed to. You will be left with a vector containing a dangling pointer.
Without changing the type of your std::vector
(which I assume is std::vector<MyPointer*>
) there are two ways to make this code exception safe.
Using C++11:
auto ptr = std::unique_ptr<MyPointer>(new MyPointer());
vector.emplace_back(ptr.get());
ptr.release();
Or the more verbose C++03 way:
MyPointer* ptr = new MyPointer();
try
{
vector.push_back(ptr);
}
catch (...)
{
delete ptr;
throw;
}
If you are able to change the type of your std::vector<MyPointer*>
then the easiest method are those suggested by Daniel Frey above:
std::vector<std::unique_ptr<MyPointer>> vector;
vector.emplace_back(std::unique_ptr<MyPointer>(new MyPointer()));
Or with C++14:
std::vector<std::unique_ptr<MyPointer>> vector;
vector.emplace_back(std::make_unique<MyPointer>());