8
votes

C++11 (and C++14) STL containers have noexcept destructors and clear() member functions.

This implies that the elements should have noexcept destructors too, or at least that the actual elements stored in containers should not throw any exceptions when destroyed—or more correctly, that the corresponding allocator_traits<Allocator>::destroy call should not throw.

Is this specified as a requirement (explicitly or implied by another explicit requirement) anywhere in the standard? If not, why?

I know is_nothrow_constructible requires a noexcept destructor, but is_constructible alone doesn’t, and container requirements are specified in terms of concepts rather than type traits templates anyway.

1
The container destructor could catch and ignore any exceptions from the element's destructor(s). - Richard Critten

1 Answers

7
votes

17.6.4.8/1 In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program. If these components do not meet their requirements, the Standard places no requirements on the implementation.

17.6.4.8/2 In particular, the effects are undefined in the following cases:

...

  • if any replacement function or handler function or destructor operation exits via an exception, unless specifically allowed in the applicable Required behavior: paragraph.