I have a function that can be reduced to this:
void f() {
static MyObject o("hello");
DoSomethingWith(o);
}
This function is called across a C API boundary, so like a good boy, I use try
to catch any exceptions that are thrown before they cross the boundary and screw things up:
void f() {
try {
static MyObject o("hello");
DoSomethingWith(o);
} catch (const MyObjectException& e) {
Message("Constructor of o failed");
}
}
This function is called 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 am using 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 a static
function variable terminates unusually (by throw
ing, a longjmp
out of the constructor, termination of the thread that it's in, etc)? Also what should happen with any other static
variables declared before and after it? I would appreciate any relevant quotes from the standard as well.