Inspecting result<T, EC>

Suppose we will be writing a function print_half that takes an integer number (however big) represented as an std::string and outputs a number which is twice smaller: outcome::result<void> print_half(const std::string& text); View this code on Github The type result<void> means that there is no value to be retuned upon success, but that the operation might still fail, and we may be interested in inspecting the cause of the failure. The class template result<> is declared with the attribute [[nodiscard]], which means the compiler will warn you if you forget to inspect the returned object (in C++ 17 or later). »

TRY operations

In the implementation of function print_half we have seen the usage of the macro OUTCOME_TRY : OUTCOME_TRY (i, (BigInt::fromString(text))); The OUTCOME_TRY macro uses C macro overloading to select between two implementations based on the number of input parameters. If there is exactly one input parameter i.e. without the i,, the control statement is roughly equivalent to: auto&& __result = BigInt::fromString(text); if (!__result) return __result.as_failure(); Where __result is a compile time generated unique name. »