Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::expected<T,E>::operator=

      From cppreference.com
      <cpp‎ |utility‎ |expected
       
       
      Utilities library
       
       
      Primary template
      constexpr expected& operator=(const expected& other);
      (1)(since C++23)
      constexpr expected& operator=( expected&& other)
         noexcept(/* see below */);
      (2)(since C++23)
      template<class U=std::remove_cv_t<T>>
      constexpr expected& operator=( U&& v);
      (3)(since C++23)
      template<class G>
      constexpr expected& operator=(conststd::unexpected<G>& e);
      (4)(since C++23)
      template<class G>
      constexpr expected& operator=(std::unexpected<G>&& e);
      (5)(since C++23)
      void partial specialization
      constexpr expected& operator=(const expected& other);
      (6)(since C++23)
      constexpr expected& operator=( expected&& other)
         noexcept(/* see below */);
      (7)(since C++23)
      template<class G>
      constexpr expected& operator=(conststd::unexpected<G>& e);
      (8)(since C++23)
      template<class G>
      constexpr expected& operator=(std::unexpected<G>&& e);
      (9)(since C++23)
      Helper function template
      template<class T,class U,class...Args>
      constexprvoid/*reinit-expected*/( T& newval, U& oldval, Args&&...args)
      (10)(since C++23)
      (exposition only*)

      Assigns a new value to an existingexpected object.

      Contents

      [edit]Parameters

      other - anotherexpected object whose contained value to assign
      v - value to assign to the contained value
      e -std::unexpected object whose contained value to assign
      newval - the contained value to be constructed
      oldval - the contained value to be destroyed
      args - the arguments used as initializers ofnewval

      [edit]Effects

      [edit]Primary template assignment operators

      1,2) Assigns the state ofother to*this.
      Ifhas_value() andrhs.has_value() have different values (i.e. one of*this andother contains an expected valueval and the other contains an unexpected valueunex ), the exposition-only function templatereinit-expected is called to safely update the state.
      1) The contained value is assigned as follows:
      Value of
       has_value() 
      Value ofother.has_value()
      truefalse
      trueval =*other;reinit-expected
          (unex, val, other.error());
      falsereinit-expected
          (val, unex,*other);
      unex = other.error();
      2) The contained value is assigned as follows:
      Value of
       has_value() 
      Value ofother.has_value()
      truefalse
      trueval = std::move(*other);reinit-expected
          (unex, val, std::move(other.error()));
      falsereinit-expected
          (val, unex,
           std::move(*other));
      unex = std::move(other.error());
      Then, if no exception was thrown, executeshas_val = other.has_value();.
      3) The expected value is assigned as follows:
      Value of
       has_value() 
      Equivalent to
      trueval =std::forward<U>(v);
      falsereinit-expected(val, unex,std::forward<U>(v));
      has_val =false;
      4,5) The unexpected value is assigned as follows:
       Overload Value of
       has_value() 
      Equivalent to
      (4)truereinit-expected(val, unex,std::forward<const G&>(e.error()));
      has_val =false;
      falseunex =std::forward<const G&>(e.error());
      (5)truereinit-expected(val, unex,std::forward<G>(e.error()));
      has_val =false;
      falseunex =std::forward<G>(e.error());

      [edit]void partial specialization assignment operators

      6) The unexpected value is assigned or destroyed as follows:
      Value of
       has_value() 
      Value ofother.has_value()
      truefalse
      true(no effects)std::construct_at
          (std::addressof(unex), rhs.unex);
      has_val =false;
      falsestd::destroy_at(std::addressof(unex));
      has_val =true;
      unex = other.error();
      7) The unexpected value is assigned or destroyed as follows:
      Value of
       has_value() 
      Value ofother.has_value()
      truefalse
      true(no effects)std::construct_at
          (std::addressof(unex),
           std::move(rhs.unex));
      has_val =false;
      falsestd::destroy_at(std::addressof(unex));
      has_val =true;
      unex = std::move(other.error());
      8,9) The unexpected value is assigned as follows:
       Overload Value of
       has_value() 
      Equivalent to
      (8)truestd::construct_at(std::addressof(unex),
                        std::forward<const G&>(e.error()));
      has_val =false;
      falseunex =std::forward<const G&>(e.error());
      (9)truestd::construct_at(std::addressof(unex),std::forward<G>(e.error()));
      has_val =false;
      falseunex =std::forward<G>(e.error());

      [edit]Helper function template

      The exposition-only function templatereinit-expected is “defined” as follows:

      template<class NewType,class OldType,class...Args>constexprvoid reinit-expected(NewType& new_val, OldType& old_val, Args&&...args){// Case 1: the construction of “new_val” is non-throwing:// “new_val” can be directly constructed after destroying “old_val”ifconstexpr(std::is_nothrow_constructible_v<NewType, Args...>){std::destroy_at(std::addressof(old_val));std::construct_at(std::addressof(new_val),std::forward<Args>(args)...);}// Case 2: the move construction of “new_val” is non-throwing:// constuct a temporary NewType object first// (“old_val” is left intact if an exception is thrown from this construction)elseifconstexpr(std::is_nothrow_move_constructible_v<NewType>){        NewType temp(std::forward<Args>(args)...);// may throwstd::destroy_at(std::addressof(old_val));std::construct_at(std::addressof(new_val), std::move(temp));}// Case 3: the construction of “new_val” is potentially-throwing:// a backup of “old_val” is required in order to recover from an exceptionelse{        OldType temp(std::move(old_val));// may throwstd::destroy_at(std::addressof(old_val));try{std::construct_at(std::addressof(new_val),std::forward<Args>(args)...);// may throw}catch(...){std::construct_at(std::addressof(old_val), std::move(temp));throw;}}}

      This function template is called when the assignment is going to make*this hold the alternative value (i.e. from expected value to unexpected value, or from unexpected value to expected value).

      In this case, the old valueoldval needs to be destroyed before constructing the new valuenewval. However, the construction ofnewval may throw an exception. In order to providestrong exception safety guarantee, the old value needs to be restored before rethrowing the exception so that*this will have a valid state while the exception is being handled.

      [edit]Return value

      1-9)*this

      [edit]Constraints and supplement information

      [edit]Primary template assignment operators

      1) This overload is defined as deleted unless all following values aretrue:
      2) This overload participates in overload resolution only if all following values aretrue:
      3) This overload participates in overload resolution only if all following conditions are satisfied:
      4) This overload participates in overload resolution only if all following values aretrue:
      5) This overload participates in overload resolution only if all following values aretrue:

      [edit]void partial specialization assignment operators

      6) This overload is defined as deleted unlessstd::is_copy_assignable_v<E> andstd::is_copy_constructible_v<E> are bothtrue.
      7) This overload participates in overload resolution only ifstd::is_move_constructible_v<E> andstd::is_move_assignable_v<E> are bothtrue.
      8) This overload participates in overload resolution only ifstd::is_constructible_v<E,const G&> andstd::is_assignable_v<E&,const G&> are bothtrue.
      9) This overload participates in overload resolution only ifstd::is_constructible_v<E, G> andstd::is_assignable_v<E&, G> are bothtrue.

      [edit]Exceptions

      2)
      7)

      [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 3886C++23the default template argument of overload(3) wasTchanged tostd::remove_cv_t<T>
      LWG 4025C++23overload(7) was defined as deleted ifE is not
      move constructible or not move assignable
      it does not participate in
      overload resolution in this case

      [edit]See also

      constructs the expected value in-place
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/expected/operator%3D&oldid=183104"

      [8]ページ先頭

      ©2009-2025 Movatter.jp