Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use emplace_back with a container of unique_ptrs?

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 any InputIterator 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_ptrs)

like image 932
Billy ONeal Avatar asked Nov 01 '12 07:11

Billy ONeal


People also ask

When should I use Emplace_back?

Specific use case for emplace_back : If you need to create a temporary object which will then be pushed into a container, use emplace_back instead of push_back . It will create the object in-place within the container.

Does Emplace_back use move constructor?

Calling emplace_back will call the move constructor of std::string when std::move is used, which could save on a copy (so long as that string isn't stored in a SSO buffer). Note that this is essentially the same as push_back in this case.


1 Answers

If reallocation is required and it fails, then yes, your object never went into the container and will thus be lost.

However, it should be noted that this is pure user error. emplace_back should not be "banned" for containers of unique_ptr, because there are perfectly safe ways of doing this (such as reserveing the space beforehand, so you know it will always be there). Also, you can pass in whole unique_ptrs, since it's perfectly capable of using a move constructor.

So really, it's your fault for not properly wrapping your non-RAII object (the int*) in a RAII object before the point where you could throw exceptions.

like image 74
Nicol Bolas Avatar answered Oct 05 '22 17:10

Nicol Bolas