Home | History | Annotate | Download | only in util.smartptr.shared.const
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <memory>
     11 
     12 // shared_ptr
     13 
     14 // shared_ptr(shared_ptr&& r);
     15 
     16 #include <memory>
     17 #include <cassert>
     18 
     19 struct A
     20 {
     21     static int count;
     22 
     23     A() {++count;}
     24     A(const A&) {++count;}
     25     ~A() {--count;}
     26 };
     27 
     28 int A::count = 0;
     29 
     30 int main()
     31 {
     32     {
     33         std::shared_ptr<A> pA(new A);
     34         assert(pA.use_count() == 1);
     35         assert(A::count == 1);
     36         {
     37             A* p = pA.get();
     38             std::shared_ptr<A> pA2(std::move(pA));
     39             assert(A::count == 1);
     40 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     41             assert(pA.use_count() == 0);
     42             assert(pA2.use_count() == 1);
     43 #else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
     44             assert(pA.use_count() == 2);
     45             assert(pA2.use_count() == 2);
     46 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
     47             assert(pA2.get() == p);
     48         }
     49 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     50         assert(pA.use_count() == 0);
     51         assert(A::count == 0);
     52 #else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
     53         assert(pA.use_count() == 1);
     54         assert(A::count == 1);
     55 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
     56     }
     57     assert(A::count == 0);
     58     {
     59         std::shared_ptr<A> pA;
     60         assert(pA.use_count() == 0);
     61         assert(A::count == 0);
     62         {
     63             std::shared_ptr<A> pA2(std::move(pA));
     64             assert(A::count == 0);
     65             assert(pA.use_count() == 0);
     66             assert(pA2.use_count() == 0);
     67             assert(pA2.get() == pA.get());
     68         }
     69         assert(pA.use_count() == 0);
     70         assert(A::count == 0);
     71     }
     72     assert(A::count == 0);
     73 }
     74