The C++ standard sometimes uses the phrase "shall not throw exceptions", for instance in 17.6.3.4 when enumerating the Hash
requirements. Does this imply that a standard-conforming implementation must mark the call operator of std::hash
as noexcept
or does this just implies that throwing from within a hash functor incurs in undefined or implementation-defined behaviour?
I checked that both libstdc++ and libc++ mark std::hash
's call operator as noexcept
, but I would like to understand if this is a required behaviour or not.
"Requires: Thou shalt not throw" means exactly what you guessed: If you throw, you will get undefined behavior.
Furthermore 17.6.5.12 [res.on.exception.handling]/p1 lets implementors add a noexcept-specfication:
Any of the functions defined in the C++ standard library can report a failure by throwing an exception of a type described in its Throws: paragraph. An implementation may strengthen the exception specification for a non-virtual function by adding a non-throwing noexcept-specification.
libstdc++ and libc++ mark std::hash
's call operator as noexcept
as a conforming extension. They are allowed, but not required to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With