ęœ€ēµ‚ę›“ę–°ę—„ę™‚:
ćŒę›“ę–°

屄歓 編集

function template
<memory>

std::swap (éžćƒ”ćƒ³ćƒé–¢ę•°)(C++11)

namespace std {
  template <class T>
  void
    swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept; // (1) C++11
  template <class T>
  constexpr void
    swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept; // (1) C++26
}

ꦂ要

2恤恮weak_ptrć‚Ŗćƒ–ć‚øć‚§ć‚Æćƒˆć‚’å…„ć‚Œę›æćˆć‚‹ć€‚

åŠ¹ęžœ

a.swap(b)

ęˆ»ć‚Šå€¤

なし

例外

ęŠ•ć’ćŖć„

例

#include <iostream>
#include <memory>

int main()
{
  std::shared_ptr<int> sp1(new int(1));
  std::weak_ptr<int> wp1 = sp1;

  std::shared_ptr<int> sp2(new int(2));
  std::weak_ptr<int> wp2 = sp2;

  if (std::shared_ptr<int> r = wp1.lock()) {
    std::cout << r << std::endl;
  }

  if (std::shared_ptr<int> r = wp2.lock()) {
    std::cout << r << std::endl;
  }

  // wp1とwp2ć‚’å…„ć‚Œę›æćˆć‚‹
  std::swap(wp1, wp2);

  if (std::shared_ptr<int> r = wp1.lock()) {
    std::cout << r << std::endl;
  }

  if (std::shared_ptr<int> r = wp2.lock()) {
    std::cout << r << std::endl;
  }
}

å‡ŗåŠ›ä¾‹

0x14ab010
0x14ab060
0x14ab060
0x14ab010

ćƒćƒ¼ć‚øćƒ§ćƒ³

čØ€čŖž

  • C++11

処理系

å‚ē…§