3 ms·
Could you compile methods with error codes into two versions - one that handles error codes and one that ignores them. Then when you want to return an error cod
by chrisseaton 6y ago
Could you compile methods with error codes into two versions - one that handles error codes and one that ignores them. Then when you want to return an error code, you also transfer control to the version of each method that handles the codes.
- wahern 6y agoThat's basically how exceptions work in a modern C++ ABI. And it's why taking an exception is so slow--throw has to lookup the caller in a huge map to find the address of the caller's catch handler to jump to, then that function has to do the same, etc, all the way up the stack. In principal exceptions should be much faster than explicit error checking, and as in the article this is often the case. But modern CPUs are really good at speculating through conditional branches. To achieve "zero cost" exceptions compilers often have to do non-trivial code reordering and duplication to segregate the non-exception and exception branches. Sometimes this necessary rewriting can have a negative impact on performance.
- Gibbon1 6y agoOne feature I've wanted is an error return keyword to complement 'return val;' something like 'error_return val;' Then a feature where if a function does an error return but the caller doesn't check the error the compiler inserts a exception or a panic. So you could write code like log_stuff(stuff); // on an error will panic. var rtn = log_stuff(stuff); if(rtn.error) { // handle error } try { log_stuff(stuff); } catch() { // jumps here on an error }