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