Home | History | Annotate | Download | only in unique.ptr.single.modifiers
      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 // unique_ptr
     13 
     14 // test reset
     15 
     16 #include <memory>
     17 #include <cassert>
     18 
     19 struct A
     20 {
     21     static int count;
     22     A() {++count;}
     23     A(const A&) {++count;}
     24     virtual ~A() {--count;}
     25 };
     26 
     27 int A::count = 0;
     28 
     29 struct B
     30     : public A
     31 {
     32     static int count;
     33     B() {++count;}
     34     B(const B&) {++count;}
     35     virtual ~B() {--count;}
     36 };
     37 
     38 int B::count = 0;
     39 
     40 int main()
     41 {
     42     {
     43     std::unique_ptr<A> p(new A);
     44     assert(A::count == 1);
     45     assert(B::count == 0);
     46     A* i = p.get();
     47     p.reset(new B);
     48     assert(A::count == 1);
     49     assert(B::count == 1);
     50     }
     51     assert(A::count == 0);
     52     assert(B::count == 0);
     53     {
     54     std::unique_ptr<A> p(new B);
     55     assert(A::count == 1);
     56     assert(B::count == 1);
     57     A* i = p.get();
     58     p.reset(new B);
     59     assert(A::count == 1);
     60     assert(B::count == 1);
     61     }
     62     assert(A::count == 0);
     63     assert(B::count == 0);
     64 }
     65