Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::optional<T>::operator=

      From cppreference.com
      <cpp‎ |utility‎ |optional
       
       
      Utilities library
       
       
      optional& operator=(std::nullopt_t)noexcept;
      (1)(since C++17)
      (constexpr since C++20)
      constexpr optional& operator=(const optional& other);
      (2)(since C++17)
      constexpr optional& operator=
         ( optional&& other)noexcept(/* see below */);
      (3)(since C++17)
      template<class U>
      optional& operator=(const optional<U>& other);
      (4)(since C++17)
      (constexpr since C++20)
      template<class U>
      optional& operator=( optional<U>&& other);
      (5)(since C++17)
      (constexpr since C++20)
      template<class U=std::remove_cv_t<T>>
      optional& operator=( U&& value);
      (6)(since C++17)
      (constexpr since C++20)

      Replaces contents of*this with the contents ofother.

      1) If*this contains a value, callsval ->T::~T() to destroy the contained value; otherwise no effect.*this does not contain a value after this call.
      2-5) Assigns the state ofother.has_value() returnsother.has_value() after this call.
      Effect*this contains a value*this does not contain a value
      other contains a value
      • for overloads(2,4), assigns*other to the contained value
      • for overloads(3,5), assignsstd::move(*other) to the contained value
      • for overloads(2,4),direct-non-list-initializes the contained value with*other
      • for overloads(3,5), direct-non-list-initializes the contained value withstd::move(*other)
      other does not contain a valuedestroys the contained value by callingval ->T::~T()no effect
      2) Ifstd::is_copy_constructible_v<T> orstd::is_copy_assignable_v<T> isfalse, the assignment operator is defined as deleted.
      Ifstd::is_trivially_copy_constructible_v<T>,std::is_trivially_copy_assignable_v<T> andstd::is_trivially_destructible_v<T> are alltrue, the assignment operator is trivial.
      3) This overload participates in overload resolution only ifstd::is_move_constructible_v<T> andstd::is_move_assignable_v<T> are bothtrue.
      Ifstd::is_trivially_move_constructible_v<T>,std::is_trivially_move_assignable_v<T> andstd::is_trivially_destructible_v<T> are alltrue, the assignment operator is trivial.
      4,5) These overloads participate in overload resolution only if all following conditions are satisfied:
      6) If*this contains a value, assignsstd::forward<U>(value) to the contained value; otherwise direct-non-list-initializes the contained value withstd::forward<U>(value).*this contains a value after this call.
      This overload participates in overload resolution only if all following conditions are satisfied:
      1. In other words,T is not constructible, convertible, or assignable from any expression of type (possibly const-qualified)std::optional<U>

      Contents

      [edit]Parameters

      other - anotheroptional object whose contained value to assign
      value - value to assign to the contained value

      [edit]Return value

      *this

      [edit]Exceptions

      2-6) Throws any exception thrown by the constructor or assignment operator ofT. If an exception is thrown, the initialization state of*this (and ofother in case of(2-5)) is unchanged, i.e. if the object contained a value, it still contains a value, and the other way round. The contents ofvalue and the contained values of*this andother depend on the exception safety guarantees of the operation from which the exception originates (copy-constructor, move-assignment, etc.).
      3) Has following
      noexcept specification:  

      [edit]Notes

      An optional objectop may be turned into an empty optional with bothop={}; andop= nullopt;. The first expression constructs an emptyoptional object with{} and assigns it toop.

      Feature-test macroValueStdFeature
      __cpp_lib_optional202106L(C++20)
      (DR20)
      Fullyconstexpr(1),(4-6)

      [edit]Example

      Run this code
      #include <iostream>#include <optional> int main(){std::optional<constchar*> s1="abc", s2;// constructor    s2= s1;// assignment    s1="def";// decaying assignment (U = char[4], T = const char*)std::cout<<*s2<<' '<<*s1<<'\n';}

      Output:

      abc def

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3886C++17the default template argument of overload(6) wasTchanged tostd::remove_cv_t<T>
      P0602R4C++17copy/move assignment operator may not be trivial
      even if underlying operations are trivial
      required to propagate triviality
      P2231R1C++20overloads(1,4-6) were notconstexprmadeconstexpr

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp