It seems to me that if you have some C++ code like this:
int f()
{
try {
if( do_it() != success ) {
throw do_it_failure();
}
} catch( const std::exception &e ) {
show_error( e.what() );
}
}
The C++ compiler should be able to optimize the throw and catch into almost a simple goto.
However, it seems to me from my experience viewing disassembly and stepping through code that the compilers always jump through the very messy exception handling libraries.
Why do they do that? Is there some language requirement that prevents optimizing? What if it was:
int f()
{
try { throw std::runtime_error("Boo!"); }
catch ( const std::exception &e ) { std::cout << e.what() << std::endl; }
}
Why does the compiler not just rewrite that as
int f()
{
std::cout << "Boo!" << std::endl;
}