So far in this tutorial, type
result<T, EC> has always been a
default_policy<T, EC, EP>
in fact to merely satisfy the trait
EC to be treated as if an
error_code. Outcome specialises that
hence they “just work”.
If no specialisation exists,
trait::is_error_code_available<EC> is true
if there exists some ADL discovered free function
Thus, we can in fact use any custom
EC type we like, including one carrying additional
information, or payload. This payload can carry anything you like, and you can tell
Outcome to do various things with that payload under various circumstances. For example:
- If the user tries to observe an unsuccessful
result, throw a custom exception containing the cause of failure with accompanying context from the payload.
- If the user ever constructs an
outcomefrom a payload carrying
result, set the exception ptr in the constructed
outcometo a custom exception containing the cause of the failure with accompanying context from the payload.
- Transmit a stack backtrace specifying the exact point at which failure occurred, symbolising that backtrace into human readable text at the moment of conversion into human readable text.
- Upon a namespace-localised
resultfrom library A being copy/moved into a namespace-localised
resultfrom C bindings library B, set the C
errnoif the error code and category map onto the
There are many, many other options of course. This tutorial can only cover a
reasonable subset. This section covers Example 1 above, throwing custom exceptions
with payload upon observation of an unsuccessful