swap.pass.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <memory>
  9. // weak_ptr
  10. // void swap(weak_ptr& r);
  11. #include <memory>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. struct A
  15. {
  16. static int count;
  17. A() {++count;}
  18. A(const A&) {++count;}
  19. ~A() {--count;}
  20. };
  21. int A::count = 0;
  22. int main(int, char**)
  23. {
  24. {
  25. A* ptr1 = new A;
  26. A* ptr2 = new A;
  27. std::shared_ptr<A> p1(ptr1);
  28. std::weak_ptr<A> w1(p1);
  29. {
  30. std::shared_ptr<A> p2(ptr2);
  31. std::weak_ptr<A> w2(p2);
  32. w1.swap(w2);
  33. assert(w1.use_count() == 1);
  34. assert(w1.lock().get() == ptr2);
  35. assert(w2.use_count() == 1);
  36. assert(w2.lock().get() == ptr1);
  37. assert(A::count == 2);
  38. }
  39. }
  40. assert(A::count == 0);
  41. return 0;
  42. }