> (...) throwing an exception in a noexcept function is undefined behavior.
Small but critical correction: noexcept functions can throw exceptions. What they cannot do is allow exceptions to bubble up to function invokers. This means that it is trivial to define noexcept functions: just add a try-catch block that catches all exceptions.
Hmm... If you were reading the documentation for function foo() and it read "if the argument is negative, foo() throws an exception", would you understand that the function throws an exception and catches it internally before doing something else, or that it throws an exception that the caller must catch?
> If you were reading the documentation for function foo() and it read "if the argument is negative, foo() throws an exception" (...)
I think you didn't understood what I said.
In C++ functions declared as noexcept are expected to not allow exceptions to bubble up. If an exception bubbles up from one of these functions, the runtime calls std::terminate.
This does not mean you cannot throw and handle exceptions within such a function. You are free to handle any exception within a noexcept function. You can throw and catch as many exceptions you feel like it while executing it. You just can't let them bubble up from the scope of your function.
I think you're the one who didn't understand me. You made a correction about the meaning of the phrase "throwing an exception". The point of my question is that your correction is incorrect, because if you read the sentence "if the argument is negative, foo() throws an exception" you would indeed understand that an exception will unwind the stack out of the foo() call in such a situation. There's no difference between "foo() allows an exception to bubble up" and "foo() throws an exception"; both phrases describe the same situation.
I suppose you are technically correct that noexcept can throw to themselves. But that's just being pedantic, isn't it? From the observer/caller point of view the function won't ever throw. It will always return (or abort).
> I suppose you are technically correct that noexcept can throw to themselves. But that's just being pedantic, isn't it?
No. There are comments in this thread from people who are surprised that you can still handle exceptions within a noexcept function. Some seem to believe noexcept is supposed to mean "don't use exception within this scope". My comment is intended to clarify that, yes, you can throw and catch any exception from within a noexcept function, because noexcept does not mean "no exceptions within this scope" and instead only means "I should not allow exceptions to bubble up, and if I happen to do then just kill the app".
Small but critical correction: noexcept functions can throw exceptions. What they cannot do is allow exceptions to bubble up to function invokers. This means that it is trivial to define noexcept functions: just add a try-catch block that catches all exceptions.