No-value policies

In the previous section we have seen that it would be useful if calling member function .value() on object of type outcome<T> that did not contain a value, would cause an exception to be thrown according to some user-defined policy.

Let us consider result<T> first. It is an alias to basic_result<T, E, NoValuePolicy> , where E is the type storing error information and defaulted to std::error_code/boost::system::error_code, and NoValuePolicy is a no-value policy defaulted to default_policy<T, EC, EP> .

The semantics of basic_result::value() are:

  1. Calls NoValuePolicy::wide_value_check(*this).
  2. Return a reference to the contained value. If no value is actually stored, your program has entered undefined behaviour.

Thus, the semantics of function .value() depend on the no-value policy. The default policy (policy::default_policy<T, EC, void>) for EC of type std::error_code1 does the following:

Class templates basic_result<T, E, NoValuePolicy> and basic_outcome<T, EC, EP, NoValuePolicy>

never use exceptions. Any exception-related logic is provided exclusively through no-value policies.

When designing your own success-or-failure type using templates basic_result<> or basic_outcome<> you have to decide what no-value policy you want to use. Either create your own, or use one of the predefined policies.

You can also use one of the two other predefined aliases for basic_result<>:

  1. Similar overloads exist for throwing boost::system::system_error when EC is boost::system::error_code. [return]