Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::expected<T,E>::or_else

      From cppreference.com
      <cpp‎ |utility‎ |expected
       
       
      Utilities library
       
       
      Primary template
      template<class F>
      constexprauto or_else( F&& f)&;
      (1)(since C++23)
      template<class F>
      constexprauto or_else( F&& f)const&;
      (2)(since C++23)
      template<class F>
      constexprauto or_else( F&& f)&&;
      (3)(since C++23)
      template<class F>
      constexprauto or_else( F&& f)const&&;
      (4)(since C++23)
      void partial specialization
      template<class F>
      constexprauto or_else( F&& f)&;
      (5)(since C++23)
      template<class F>
      constexprauto or_else( F&& f)const&;
      (6)(since C++23)
      template<class F>
      constexprauto or_else( F&& f)&&;
      (7)(since C++23)
      template<class F>
      constexprauto or_else( F&& f)const&&;
      (8)(since C++23)

      If*this contains an unexpected value, invokesf with the unexpected value of*this as the argument and returns its result. Otherwise, returns astd::expected object that represents an expected value.

      1-4) The expected value is initialized with the expected valueval of*this.

      Given typeG as:

      1,2)std::remove_cvref_t<std::invoke_result_t<F, decltype(error())>>
      3,4)std::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(error()))>>
      5,6)std::remove_cvref_t<std::invoke_result_t<F, decltype(error())>>
      7,8)std::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(error()))>>

      IfG is not a specialization ofstd::expected, orstd::is_same_v<G::value_type, T> isfalse, the program is ill-formed.

      1,2) These overloads participate in overload resolution only ifstd::is_constructible_v<T, decltype((val))> istrue.
      3,4) These overloads participate in overload resolution only ifstd::is_constructible_v<T, decltype(std::move(val))> istrue.

      Contents

      [edit]Parameters

      f - a suitable function orCallable object that returns astd::expected

      [edit]Return value

       Overload Value ofhas_value()
      truefalse
      (1,2)G(std::in_place, val)std::invoke(std::forward<F>(f), error())
      (3,4)G(std::in_place, std::move(val))std::invoke(std::forward<F>(f), std::move(error()))
      (5,6)G()std::invoke(std::forward<F>(f), error())
      (7,8)std::invoke(std::forward<F>(f), std::move(error()))

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_expected202211L(C++23)Monadic functions forstd::expected

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3938C++23the expected value was obtained byvalue()[1]changed to**this
      LWG 3973C++23the expected value was obtained by**this[2]changed toval
      1. value() requiresE to be copy constructible (seeLWG issue 3843), whereoperator* does not.
      2. **this can triggerargument-dependent lookup.

      [edit]See also

      returns theexpected itself if it contains an expected value; otherwise, returns anexpected containing the transformed unexpected value
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/expected/or_else&oldid=173476"

      [8]ページ先頭

      ©2009-2025 Movatter.jp