Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::optional<T>::operator=

      From cppreference.com
      <cpp‎ |experimental‎ |optional
       
       
       
       
       
      optional& operator=(std::experimental::nullopt_t)noexcept;
      (1)(library fundamentals TS)
      optional& operator=(const optional& other);
      (2)(library fundamentals TS)
      optional& operator=( optional&& other)noexcept(/* see below */);
      (3)(library fundamentals TS)
      template<class U>
      optional& operator=( U&& value);
      (4)(library fundamentals TS)

      Replaces contents of*this with the contents ofother.

      1) If*this contains a value before the call, the contained value is destroyed by calling its destructor as if byval->T::~T().*this does not contain a value after this call.
      2,3) Assigns the state ofother.
      • If both*this andother do not contain a value, the function has no effect.
      • If*this contains a value, butother does not, then the contained value is destroyed by calling its destructor.*this does not contain a value after the call.
      • Ifother contains a value, then depending on whether*this contains a value, the contained value is eitherdirect-initialized or assigned from*other(2) orstd::move(*other)(3). Note that a moved-from optional stillcontains a value.
      4) Decay-only perfect-forwarded assignment: depending on whether*this contains a value before the call, the contained value is either direct-initialized fromstd::forward<U>(value) or assigned fromstd::forward<U>(value). The function does not participate in overload resolution unlessstd::is_same<std::decay_t<U>, T>::value istrue.

      Contents

      [edit]Parameters

      other - anotheroptional object whose contained value to assign
      value - value to assign to the contained value
      Type requirements
      -
      T must meet the requirements ofCopyAssignable andCopyConstructible in order to use overload (2).
      -
      T must meet the requirements ofMoveAssignable andMoveConstructible in order to use overload (3).

      [edit]Return value

      *this

      [edit]Exceptions

      2-4) 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)) 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 the followingnoexcept declaration:
      noexcept specification:  

      [edit]Notes

      An optional objectop may be turned into an empty optional with bothop={}; andop= nullopt;.

      [edit]Example

      Run this code
      #include <experimental/optional>#include <iostream> int main(){std::experimental::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]See also

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

      [8]ページ先頭

      ©2009-2026 Movatter.jp