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 // UNSUPPORTED: c++98, c++03
     11 
     12 // <memory>
     13 
     14 // shared_ptr
     15 
     16 // template<class Y> shared_ptr(shared_ptr<Y>&& r);
     17 
     18 #include <memory>
     19 #include <type_traits>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 
     24 struct B
     25 {
     26     static int count;
     27 
     28     B() {++count;}
     29     B(const B&) {++count;}
     30     virtual ~B() {--count;}
     31 };
     32 
     33 int B::count = 0;
     34 
     35 struct A
     36     : public B
     37 {
     38     static int count;
     39 
     40     A() {++count;}
     41     A(const A&) {++count;}
     42     ~A() {--count;}
     43 };
     44 
     45 int A::count = 0;
     46 
     47 struct C
     48 {
     49     static int count;
     50 
     51     C() {++count;}
     52     C(const C&) {++count;}
     53     virtual ~C() {--count;}
     54 };
     55 
     56 int C::count = 0;
     57 
     58 int main()
     59 {
     60     static_assert(( std::is_convertible<std::shared_ptr<A>, std::shared_ptr<B> >::value), "");
     61     static_assert((!std::is_convertible<std::shared_ptr<B>, std::shared_ptr<A> >::value), "");
     62     static_assert((!std::is_convertible<std::shared_ptr<A>, std::shared_ptr<C> >::value), "");
     63     {
     64         std::shared_ptr<A> pA(new A);
     65         assert(pA.use_count() == 1);
     66         assert(B::count == 1);
     67         assert(A::count == 1);
     68         {
     69             B* p = pA.get();
     70             std::shared_ptr<B> pB(std::move(pA));
     71             assert(B::count == 1);
     72             assert(A::count == 1);
     73 #if TEST_STD_VER >= 11
     74             assert(pB.use_count() == 1);
     75             assert(pA.use_count() == 0);
     76 #else
     77             assert(pB.use_count() == 2);
     78             assert(pA.use_count() == 2);
     79 #endif
     80             assert(p == pB.get());
     81         }
     82 #if TEST_STD_VER >= 11
     83         assert(pA.use_count() == 0);
     84         assert(B::count == 0);
     85         assert(A::count == 0);
     86 #else
     87         assert(pA.use_count() == 1);
     88         assert(B::count == 1);
     89         assert(A::count == 1);
     90 #endif
     91     }
     92     assert(B::count == 0);
     93     assert(A::count == 0);
     94     {
     95         std::shared_ptr<A> pA;
     96         assert(pA.use_count() == 0);
     97         assert(B::count == 0);
     98         assert(A::count == 0);
     99         {
    100             std::shared_ptr<B> pB(pA);
    101             assert(B::count == 0);
    102             assert(A::count == 0);
    103             assert(pB.use_count() == 0);
    104             assert(pA.use_count() == 0);
    105             assert(pA.get() == pB.get());
    106         }
    107         assert(pA.use_count() == 0);
    108         assert(B::count == 0);
    109         assert(A::count == 0);
    110     }
    111     assert(B::count == 0);
    112     assert(A::count == 0);
    113 }
    114