reset_pointer.pass.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // shared_ptr
  10. // template<class Y> void reset(Y* p);
  11. #include <memory>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. struct B
  15. {
  16. static int count;
  17. B() {++count;}
  18. B(const B&) {++count;}
  19. virtual ~B() {--count;}
  20. };
  21. int B::count = 0;
  22. struct A
  23. : public B
  24. {
  25. static int count;
  26. A() {++count;}
  27. A(const A&) {++count;}
  28. ~A() {--count;}
  29. };
  30. int A::count = 0;
  31. int main(int, char**)
  32. {
  33. {
  34. std::shared_ptr<B> p(new B);
  35. A* ptr = new A;
  36. p.reset(ptr);
  37. assert(A::count == 1);
  38. assert(B::count == 1);
  39. assert(p.use_count() == 1);
  40. assert(p.get() == ptr);
  41. }
  42. assert(A::count == 0);
  43. {
  44. std::shared_ptr<B> p;
  45. A* ptr = new A;
  46. p.reset(ptr);
  47. assert(A::count == 1);
  48. assert(B::count == 1);
  49. assert(p.use_count() == 1);
  50. assert(p.get() == ptr);
  51. }
  52. assert(A::count == 0);
  53. return 0;
  54. }