Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::optional<T>::swap

      From cppreference.com
      <cpp‎ |utility‎ |optional
       
       
      Utilities library
       
       
      void swap( optional& other)noexcept(/* see below */);
      (since C++17)
      (constexpr since C++20)

      Swaps the contents with those ofother.

      • If neither*this norother contain a value, the function has no effect.
      • If only one of*this andother contains a value (let's call this objectin and the otherun), the contained value ofun isdirect-initialized fromstd::move(*in), followed by destruction of the contained value ofin as if byin->T::~T(). After this call,in does not contain a value;un contains a value.
      • If both*this andother contain values, the contained values are exchanged by callingusingstd::swap; swap(**this,*other).

      The program is ill-formed unless typeT isSwappable andstd::is_move_constructible_v<T> istrue.

      Contents

      [edit]Parameters

      other - theoptional object to exchange the contents with

      [edit]Return value

      (none)

      [edit]Exceptions

      noexcept specification:  

      In the case of thrown exception, the states of the contained values of*this andother are determined by the exception safety guarantees ofswap of typeT orT's move constructor, whichever is called. For both*this andother, if the object contained a value, it is left containing a value, and the other way round.

      Feature-test macroValueStdFeature
      __cpp_lib_optional202106L(C++20)
      (DR20)
      Fullyconstexpr

      [edit]Example

      Run this code
      #include <iostream>#include <optional>#include <string> int main(){std::optional<std::string> opt1("First example text");std::optional<std::string> opt2("2nd text"); enum Swap{ Before, After};auto print_opts=[&](Swap e){std::cout<<(e== Before?"Before swap:\n":"After swap:\n");std::cout<<"opt1 contains '"<< opt1.value_or("")<<"'\n";std::cout<<"opt2 contains '"<< opt2.value_or("")<<"'\n";std::cout<<(e== Before?"---SWAP---\n":"\n");};     print_opts(Before);    opt1.swap(opt2);    print_opts(After); // Swap with only 1 set    opt1="Lorem ipsum dolor sit amet, consectetur tincidunt.";    opt2.reset();     print_opts(Before);    opt1.swap(opt2);    print_opts(After);}

      Output:

      Before swap:opt1 contains 'First example text'opt2 contains '2nd text'---SWAP---After swap:opt1 contains '2nd text'opt2 contains 'First example text' Before swap:opt1 contains 'Lorem ipsum dolor sit amet, consectetur tincidunt.'opt2 contains ''---SWAP---After swap:opt1 contains ''opt2 contains 'Lorem ipsum dolor sit amet, consectetur tincidunt.'

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P2231R1C++20swap was notconstexpr while the required operations can beconstexpr in C++20madeconstexpr

      [edit]See also

      specializes thestd::swap algorithm
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/optional/swap&oldid=177184"

      [8]ページ先頭

      ©2009-2025 Movatter.jp