Details
-
Improvement
-
Status: In Progress
-
Critical
-
Resolution: Unresolved
-
4.2.1
-
None
-
Inefficiency
Description
When a compiler cannot determine whether a function declared without an explicit exception specification may throw an exception or not (e.g., because the compiler/optimizer doesn't have access to the definition of the function) it must assume that the function may throw and may need to generate suboptimal code as a result.
Compilers often assume that even inline functions that can be proven not to throw exceptions may throw unless they are declared with the empty exception specification and generates suboptimal code as a result. For example, HP aCC 6 assumes that S::S() in the code below may throw
and fails to optimize the catch block away, generating object code 2.5 times the size bigger than necessary. (Note that gcc 3.4 on IPF generates optimal code in this case).
To help compilers generate optimal code we should make use of the empty exception specification on all functions that cannot throw exceptions, including those declared explicitly or implicitly inline.
#include <new> struct S { int i; S () /* throw () */: i () { } }; void uninit_fill (S *a, S *b) { S *p = a; try { for ( ; p != b; ++p) new (p) S (); } catch (...) { while (p-- != a) p->~S (); throw; } }