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(pointer, deleter) ctor
     15 
     16 // unique_ptr<T[], D&>(pointer, d) does not requires CopyConstructible deleter
     17 
     18 #include <memory>
     19 #include <cassert>
     20 
     21 struct A
     22 {
     23     static int count;
     24     A() {++count;}
     25     A(const A&) {++count;}
     26     ~A() {--count;}
     27 };
     28 
     29 int A::count = 0;
     30 
     31 class Deleter
     32 {
     33     int state_;
     34 
     35     Deleter(const Deleter&);
     36     Deleter& operator=(const Deleter&);
     37 public:
     38 
     39     Deleter() : state_(5) {}
     40 
     41     int state() const {return state_;}
     42     void set_state(int s) {state_ = s;}
     43 
     44     void operator()(A* p) {delete [] p;}
     45 };
     46 
     47 int main()
     48 {
     49     {
     50     A* p = new A[3];
     51     assert(A::count == 3);
     52     Deleter d;
     53     std::unique_ptr<A[], Deleter&> s(p, d);
     54     assert(s.get() == p);
     55     assert(s.get_deleter().state() == 5);
     56     d.set_state(6);
     57     assert(s.get_deleter().state() == 6);
     58     }
     59     assert(A::count == 0);
     60 }
     61