Concept Subspace :: sus :: ops :: Try

template <class T>
concept Try
requires
requires {
      // The Try type is not a reference, conversions require concrete types.
      requires !std::is_reference_v<T>;
      // The Output success type can be converted to/from the Try type.
      typename TryImpl<std::remove_const_t<T>>::Output;
#if !defined(sus_gcc_bug_110927_exists)
      // The Try type can be used to produce another Try type with a different
      // Output type but the same error type.
      typename TryImpl<std::remove_const_t<T>>::template RemapOutput<
          typename TryImpl<std::remove_const_t<T>>::Output>;
#endif
    }
requires(const T& t, T&& tt) {
      // is_success() reports if the Try type is in a success state.
      { TryImpl<std::remove_const_t<T>>::is_success(t) } -> std::same_as<bool>;
      // into_output() unwraps from the Try type to its success type. It may
      // assume that the input is in a success state (`is_success()` would
      // return true), as the `try_into_output()` method verifies this.
      {
        TryImpl<std::remove_const_t<T>>::into_output(::sus::move(tt))
      } -> std::same_as<typename TryImpl<std::remove_const_t<T>>::Output>;
    }
std::is_void_v<typename TryImpl<std::remove_const_t<T>>::Output> ||  //
     requires(typename TryImpl<std::remove_const_t<T>>::Output&& output) {
       // from_output() converts a success type to the Try type.
       {
         TryImpl<std::remove_const_t<T>>::from_output(
             ::sus::forward<typename TryImpl<std::remove_const_t<T>>::Output>(
                 output))
       } -> std::same_as<T>;
     }

A concept for types that can indicate success and failure.

The Try concept is implemented by specializing TryImpl for a type T with:

  • A type Output that is the unwrapped success value type.
  • A member static bool is_success(const T& t) that reports if the given t is a success or failure value.
  • A member static Output into_output(T t) that unwraps a successful T to its success value.
  • A member static T from_output(Output t) that constructs a successful T from a success value.

Note that when the Output type is void then from_output() will not be useable. To support void, the TryDefault concept can be used instead of Try. The TryDefault concept allows construction of the Try type with a default success value which can be void.