What if we had ergonomics for error codes almost equal to exceptions?
The issues with error code easy to not process and not carry any additional info are relatively easy to solve. There was a proposal to add std::expected type, which is a C++ implementation of Either/Result algebraic data type well known in FP world. Such a type would require explicit unpacking to get value and explicit checking if there's an error.
The issue with status checking boilerplate is more complex though. In fact it requires that an expression is allowed to both declare expression-local variable and have return statement:
The main problem is that C++ has strict distinction between statements and expressions, and doesn't allow to mix them. Based on this, we need to either allow 'if' statement to be expression in some cases, or allow declaration statement and return statement to be used in ternary operator:
auto result = if (auto&& __tmp__ = func_returning_result(), __tmp__.is_result())
{ std::forward<decltype(func_returning_result())>(__tmp__).get_result(); }
else { return std::forward<decltype(func_returning_result())>(__tmp__).get_error(); };auto result = (auto&& __tmp__ = func_returning_result(), __tmp__.is_result()
? std::forward<decltype(func_returning_result())>(__tmp__).get_result()
: return std::forward<decltype(func_returning_result())>(__tmp__).get_error());Thoughts on this?
I would doubt that the runtime overhead of exceptions is worse than that of checking return values and continue returning out the call stack. Especially in the no-error case it should be more efficient as those ifs have potentially large runtime penalty regardless of if the jump instruction is taken or not, while the exception unwinding code is typically very asymmetric with a minimized runtime cost for the non-throw case.So before suggesting another mechanism that simplifies writing code based on error returns I would suggest actually measuring the performance in the throwing and non-throwing case compared to a return value/if-based solution. This article is not totally clear but casts serious doubts about any speed gain from avoiding exceptions:Unfortunately (or fortunately as it is so old) the TS report linked to does not show numbers for exception handling. Maybe someone else can point to a paper detailing the performance for the different approaches?
I would doubt that the runtime overhead of exceptions is worse than that of checking return values and continue returning out the call stack. Especially in the no-error case it should be more efficient as those ifs have potentially large runtime penalty regardless of if the jump instruction is taken or not, while the exception unwinding code is typically very asymmetric with a minimized runtime cost for the non-throw case.So before suggesting another mechanism that simplifies writing code based on error returns I would suggest actually measuring the performance in the throwing and non-throwing case compared to a return value/if-based solution. This article is not totally clear but casts serious doubts about any speed gain from avoiding exceptions:Unfortunately (or fortunately as it is so old) the TS report linked to does not show numbers for exception handling. Maybe someone else can point to a paper detailing the performance for the different approaches?
If we presume that exceptions can in *some* cases be slower, then if "errors" are very common or if you require *predictable* performance characteristics, exceptions may be unacceptable.
If we presume that exceptions can in *some* cases be slower, then if "errors" are very common or if you require *predictable* performance characteristics, exceptions may be unacceptable.
That's a "performance consideration".