From man gcc
:
-fno-enforce-eh-specs
Don't generate code to check for violation of exception specifications
at run time. This option violates the C++ standard, but may be useful
for reducing code size in production builds.
When a compiler optimizes, it removes all sorts of checks, it breaks the boundaries between functions, it might even avoid things like system calls the developer put in the code in some cases. So:
- Why is exception specification violation checking so special that it cannot be skipped?
- What exactly is being checked?
- If using this switch is so useful for reducing code size, how come the C++ standard requires these checks? I thought the idea is zero-overhead abstractions whenever possible.
The (now old?) standard requires that a function declared
void f() throw(x);
must not throw any exception other thanx
(or possibly derived fromx
?). If it tries to throw something else,std::unexpected()
should be called, and probably end up in a call tostd::terminate()
to kill the program.But if the exact type of the exception cannot be determined at compile time, a run-time check might be needed to determine if the exception is of an acceptable type.
Of course, if that check is removed by this option, an incorrect exception might escape from the function. And that would be a violation of the standard.