Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Thou shalt not throw" and noexcept

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.

like image 489
bluescarni Avatar asked Dec 27 '14 19:12

bluescarni


1 Answers

"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.

like image 162
Howard Hinnant Avatar answered Nov 01 '22 08:11

Howard Hinnant