Home | History | Annotate | Download | only in unique.ptr.single.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 default unique_ptr ctor
     15 
     16 #include <memory>
     17 #include <cassert>
     18 
     19 // default unique_ptr ctor shouldn't require complete type
     20 
     21 struct A;
     22 
     23 class Deleter
     24 {
     25     int state_;
     26 
     27     Deleter(Deleter&);
     28     Deleter& operator=(Deleter&);
     29 
     30 public:
     31     Deleter() : state_(5) {}
     32 
     33     int state() const {return state_;}
     34 
     35     void operator()(A* p);
     36 };
     37 
     38 void check(int i);
     39 
     40 template <class D = std::default_delete<A> >
     41 struct B
     42 {
     43     std::unique_ptr<A, D> a_;
     44     B() {}
     45     ~B();
     46 
     47     A* get() const {return a_.get();}
     48     D& get_deleter() {return a_.get_deleter();}
     49 };
     50 
     51 int main()
     52 {
     53     {
     54     B<> s;
     55     assert(s.get() == 0);
     56     }
     57     check(0);
     58     {
     59     B<Deleter> s;
     60     assert(s.get() == 0);
     61     assert(s.get_deleter().state() == 5);
     62     }
     63     check(0);
     64 }
     65 
     66 struct A
     67 {
     68     static int count;
     69     A() {++count;}
     70     A(const A&) {++count;}
     71     ~A() {--count;}
     72 };
     73 
     74 int A::count = 0;
     75 
     76 void Deleter::operator()(A* p) {delete p;}
     77 
     78 void check(int i)
     79 {
     80     assert(A::count == i);
     81 }
     82 
     83 template <class D>
     84 B<D>::~B() {}
     85