Hacker News new | ask | show | jobs
by quuxplusone 43 days ago
> The C++ container classes assume noexcept constructors for this reason, because creating a list of size 20 calls your constructor 20 times, and the cleanup gets so much messier.

No, STL containers such as std::vector definitely do not assume (nor require) non-throwing constructors. They just do the right thing. Yes, cleanup is messier than it would otherwise be.

STL containers do require that your destructors not throw (but do not require that they be noexcept).

1 comments

Containers also give more efficient and simpler semantics if move constructors are noexcept, which they generally should be.
Ah yes, I forgot that only vector can relocate elements. (Other containers can invalidate iterators but the objects themselves aren't moved.)
They could be even more efficient if we had a way to indicate relocatability instead of having to fall back to more general move semantics.