A custom no-value policy - Boost.Outcome documentation

# A custom no-value policy

If you want your `basic_outcome<>` or `basic_result<>` instances to call `std::abort()` whenever `.value()` is called on an object that does not contain a value, or `.error()` is called on an object that does not contain an error, you will need to define your own no-value policy as follows:

    struct abort_policy : outcome::policy::base
    {
      template <class Impl> static constexpr void wide_value_check(Impl &&self)
      {
        if(!base::_has_value(std::forward<Impl>(self)))
          std::abort();
      }

      template <class Impl> static constexpr void wide_error_check(Impl &&self)
      {
        if(!base::_has_error(std::forward<Impl>(self)))
          std::abort();
      }

      template <class Impl> static constexpr void wide_exception_check(Impl &&self)
      {
        if(!base::_has_exception(std::forward<Impl>(self)))
          std::abort();
      }
    };

View this code on Github

All policies ought to inherit from `base` in order to provide your policy implementation with the internal policy API for accessing and manipulating `result` and `outcome` state.

Once the policy is defined, you have to specify it when providing your own `basic_outcome` specialization:

    template <typename T>
    using strictOutcome =  //
    outcome::basic_outcome<T, std::error_code, std::exception_ptr, abort_policy>;

View this code on Github

Last revised: February 08, 2019 at 22:18:08 UTC