Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

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

      If*this represents an expected value, invokesf and returns astd::expected object that contains an expected value, which is initialized with its result (or value-initialized if the result type isvoid). Otherwise, returns astd::expected object that contains an unexpected value, which is initialized with the unexpected value of*this.

      1-4)f is invoked with the expected valueval of*this as the argument.
      5-8)f is invoked without any argument.

      Given typeU as:

      1,2)std::remove_cv_t<std::invoke_result_t<F, decltype((val))>>
      3,4)std::remove_cv_t<std::invoke_result_t<F, decltype(std::move(val))>>
      5-8)std::remove_cv_t<std::invoke_result_t<F>>

      If any of the following conditions is satisfied, the program is ill-formed:

      • U is not a valid value type forstd::expected.
      • std::is_void_v<U> isfalse, and the following corresponding declaration is ill-formed:
      1,2)U u(std::invoke(std::forward<F>(f), val));
      3,4)U u(std::invoke(std::forward<F>(f), std::move(val)));
      5-8)U u(std::invoke(std::forward<F>(f)));


      1,2) These overloads participate in overload resolution only ifstd::is_constructible_v<E, decltype(error())> istrue.
      3,4) These overloads participate in overload resolution only ifstd::is_constructible_v<E, decltype(std::move(error()))> istrue.
      5,6) These overloads participate in overload resolution only ifstd::is_constructible_v<E, decltype(error())> istrue.
      7,8) These overloads participate in overload resolution only ifstd::is_constructible_v<E, decltype(std::move(error()))> istrue.

      Contents

      [edit]Parameters

      f - a suitable function orCallable object whose call signature returns a non-reference type

      [edit]Return value

      Given expressionexpr as:

      1,2)std::invoke(std::forward<F>(f), val)
      3,4)std::invoke(std::forward<F>(f),std::move(val))
      5-8)std::invoke(std::forward<F>(f))

      The return values are defined as follows:

       Overload Value ofhas_value()
      truefalse
      (1,2)std::expected<U, E>(std::unexpect, error())
      (3,4)std::expected<U, E>
          (std::unexpect, std::move(error()))
      (5,6)std::expected<U, E>(std::unexpect, error())
      (7,8)std::expected<U, E>
          (std::unexpect, std::move(error()))

      [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/transform&oldid=173509"

      [8]ページ先頭

      ©2009-2025 Movatter.jp