ithenoob
ithenoob

Reputation: 326

What happens when constructor throws on vector.emplace or vector.emplace_back?

I have a class which could potentially throw an exception when constructing. I need many objects of this class, and I am constructing them in a vector with emplace_back. What would happen should the constructor throw?

Upvotes: 4

Views: 667

Answers (1)

Nate Kohl
Nate Kohl

Reputation: 35944

It looks like it depends on what constructors are being called.

According to section 23.3.7.5 in the standard, calls to emplace_back should have no effect if custom constructors throw:

template <class... Args> void emplace_back(Args&&... args);

[...]

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.

Upvotes: 2

Related Questions