Home | History | Annotate | Download | only in unique.ptr.runtime.ctor
      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 unique_ptr move ctor
     15 
     16 // test move ctor.  Should only require a MoveConstructible deleter, or if
     17 //    deleter is a reference, not even that.
     18 
     19 #include <memory>
     20 #include <cassert>
     21 
     22 #include "../../deleter.h"
     23 
     24 struct A
     25 {
     26     static int count;
     27     A() {++count;}
     28     A(const A&) {++count;}
     29     ~A() {--count;}
     30 };
     31 
     32 int A::count = 0;
     33 
     34 class NCDeleter
     35 {
     36     int state_;
     37 
     38     NCDeleter(NCDeleter&);
     39     NCDeleter& operator=(NCDeleter&);
     40 public:
     41 
     42     NCDeleter() : state_(5) {}
     43 
     44     int state() const {return state_;}
     45     void set_state(int s) {state_ = s;}
     46 
     47     void operator()(A* p) {delete [] p;}
     48 };
     49 
     50 int main()
     51 {
     52     {
     53     std::unique_ptr<A[]> s(new A[3]);
     54     A* p = s.get();
     55     std::unique_ptr<A[]> s2 = std::move(s);
     56     assert(s2.get() == p);
     57     assert(s.get() == 0);
     58     assert(A::count == 3);
     59     }
     60     assert(A::count == 0);
     61     {
     62     std::unique_ptr<A[], Deleter<A[]> > s(new A[3], Deleter<A[]>(5));
     63     A* p = s.get();
     64     std::unique_ptr<A[], Deleter<A[]> > s2 = std::move(s);
     65     assert(s2.get() == p);
     66     assert(s.get() == 0);
     67     assert(A::count == 3);
     68     assert(s2.get_deleter().state() == 5);
     69     assert(s.get_deleter().state() == 0);
     70     }
     71     assert(A::count == 0);
     72     {
     73     NCDeleter d;
     74     std::unique_ptr<A[], NCDeleter&> s(new A[3], d);
     75     A* p = s.get();
     76     std::unique_ptr<A[], NCDeleter&> s2 = std::move(s);
     77     assert(s2.get() == p);
     78     assert(s.get() == 0);
     79     assert(A::count == 3);
     80     d.set_state(6);
     81     assert(s2.get_deleter().state() == d.state());
     82     assert(s.get_deleter().state() ==  d.state());
     83     }
     84     assert(A::count == 0);
     85 }
     86