Focusing on Visual C++, have you ever experienced significant performance gains in C++ code using throw() (i.e. __declspec(nothrow)) non-throwing specification?
Does it really help the optimizer?
Are there any benchmarks showing performance gains?
I found different (opposite) advice on the Internet:
Boost exception-specification rationale is against throw(), instead Larry Osterman seems to be in favor of it in his blog post: Why add a throw() to your methods?
(I’d like to clarify that I’m interested in VC++ specific code; I know that in GCC the throw() specification can actually be a “pessimization” due to run-time checks.)
P.S. Reading ATL headers, I found that throw() is used pervasively; moreover, I found a convenient C++ RAII unique_handle class in this MSDN article that uses throw() specification as well.
The MSVC compiler treats it as an optimization hint, yes.
Boost has to be cross-platform, they have to go for something that’s safe and efficient on a variety of compilers. And as the boost docs say, some compilers might generate slower code when
throw()is specified, and in many cases, compilers can deduce that no exception is thrown regardless of whether there is athrow()specification, so for Boost, the safest approach is to just never use throw-specifications.But if you’re targeting MSVC specifically, then
throw()effectively tells the compiler not to generate exception-handling code for the function, which may give a speedup in cases where the function was too complex for the compiler to determine that no exception could be thrown.