
On Tue, 19 May 2009 07:37:26 +0200, "Adam Badura" <abadura@o2.pl> wrote:
Here's a perfectly legal, perfectly defined C++03 program in a single module with a single thread that has a runtime-determined number of exceptions:
#include <iostream>
void fun(int recursions);
int main() { int n; std::cin >> n; fun(n); }
void fun(int recursions) { try { throw 0; } catch(int) { fun(recursions - 1); } }
Actually you must use the exception object in the catch so that the compiler cannot optimize it away. So this example is not correct. :)
Just because the compiler *can*, doesn't mean that it does. In fact, I very much doubt you'll find a compiler that does. Exception handling is partially implemented in support libraries, and dealing with disappearing exception objects is hard for a library. So it's still likely runtime-determined. The compiler could also analyze the program and determine that it's one big noop and completely optimize it away, except for the input statement. But by all means, print the int or use a type with a non-trivial constructor as the exception. My point is made, and nit-picking won't change that. Sebastian