Quantcast
Channel: User Zan Lynx - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Is the compiler able to optimize try/catch into a simple goto?

$
0
0

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;}

Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>