The full question fits the title. Here's a quick example:
constexpr int increment(int const value) /*noexcept?*/ {
return value + 1;
}
As far as I know, noexcept should be interpreted as "nofail" when deciding whether to mark a function with it or not. Therefore, no possible situation for throwing isn't the only reasoning which must be considered.
I'm not using C++20 so my signed overflows aren't defined yet. :)
As far as I know, noexcept should be interpreted as "nofail"
That's not correct. noexcept literally means "I promise this function will never throw an exception." There are myriad other types of failures, such as segmentation faults, illegal instructions, the calling of pure virtual functions, integer divide by zero, not to mention "Bob in accounting told me all our customer numbers consist only of digits, but I just found out our very first customer's ID was actually Q001 and it doesn't parse." None of those will necessarily result in an exception, and neither will signed integer overflow, so functions failing in those ways can still be noexcept even though they can fail--they just can't throw C++ exceptions.
You may then wonder, "What happens if an exception is thrown by a noexcept function?" In that case, std::terminate() will be called, and your program will end.
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