I have a function that can be summarized as follows:
void f() { static MyObject o("hello"); DoSomethingWith(o); }
This function is called across the border of the C API, so like a good boy, I use try
to catch any exceptions that occur before crossing the border and damaging things:
void f() { try { static MyObject o("hello"); DoSomethingWith(o); } catch (const MyObjectException& e) { Message("Constructor of o failed"); } }
This function is called for the first time, and I get the message "Constructor of o failed"
. However, later the function is called again, and I get the message again. I get the message as many times as f
is called. I use Visual C ++, so this tells me what MSVC ++ does, but not what should be done.
My question is: what should happen when the constructor of the static
function variable ends abnormally (via throw
ing, a longjmp
from the constructor, terminating the stream in which it, etc.)? Also, what should happen to any other static
variables declared before and after it? I would appreciate any relevant quotes from the standard.
source share