I'm trying to investigate the difference in generated code when I switch Visual Studio 2019 from /EHsc
(Structured and C++ exceptions only) to /EHs
(also do not assume that extern "C"
functions won't throw – ref), but I can't seem to coax VS into providing a useful testcase when I've minimised it.
I'm surprised the following doesn't do it (the assembly in both cases is identical), since the contents of the function referred to by fptr
(a possible definition being included in comments for exposition) are unknown to the optimiser.
void foo();
/*void foo()
{
throw 0;
}*/
extern "C"
void bar(void (*fptr)())
{
fptr();
}
int main()
{
try
{
bar(&foo);
}
catch (...) {}
}
Granted, it knows that any hypothetical exception will be immediately caught, and since with /EHsc
the result of this exception propagation is "undefined" per Microsoft, it "appearing to work" is of course a valid outcome. But that's not much help to me here!
So how can I perform this experiment, without introducing different translation units? Ideally I want to be able to come up with a Compiler Explorer snippet for this.
My goal is to prove that permitting extern "C"
to throw (or, rather, propagate) C++ exceptions in a well-defined manner does not have a higher runtime cost than I'm willing to accept in trade.
Yes, I am aware of general advice not to let exceptions cross module boundaries or flow through third-party C code. Yes, I am doing that anyway. Yes, that's fine in our project!