Dan Albert | 287553d | 2017-02-16 10:47:51 -0800 | [diff] [blame] | 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 converting move ctor |
| 15 | |
| 16 | #include <memory> |
| 17 | #include <utility> |
| 18 | #include <cassert> |
| 19 | |
| 20 | // test converting move ctor. Should only require a MoveConstructible deleter, or if |
| 21 | // deleter is a reference, not even that. |
| 22 | // Explicit version |
| 23 | |
| 24 | struct A |
| 25 | { |
| 26 | static int count; |
| 27 | A() {++count;} |
| 28 | A(const A&) {++count;} |
| 29 | virtual ~A() {--count;} |
| 30 | }; |
| 31 | |
| 32 | int A::count = 0; |
| 33 | |
| 34 | struct B |
| 35 | : public A |
| 36 | { |
| 37 | static int count; |
| 38 | B() {++count;} |
| 39 | B(const B&) {++count;} |
| 40 | virtual ~B() {--count;} |
| 41 | }; |
| 42 | |
| 43 | int B::count = 0; |
| 44 | |
| 45 | template <class T> |
| 46 | class CDeleter |
| 47 | { |
| 48 | int state_; |
| 49 | |
| 50 | CDeleter(CDeleter&); |
| 51 | CDeleter& operator=(CDeleter&); |
| 52 | public: |
| 53 | |
| 54 | CDeleter() : state_(5) {} |
| 55 | |
| 56 | int state() const {return state_;} |
| 57 | void set_state(int s) {state_ = s;} |
| 58 | |
| 59 | void operator()(T* p) {delete p;} |
| 60 | }; |
| 61 | |
| 62 | int main() |
| 63 | { |
| 64 | { |
| 65 | CDeleter<A> d; |
| 66 | std::unique_ptr<B, CDeleter<A>&> s(new B, d); |
| 67 | A* p = s.get(); |
| 68 | std::unique_ptr<A, CDeleter<A>&> s2(s); |
| 69 | assert(s2.get() == p); |
| 70 | assert(s.get() == 0); |
| 71 | assert(A::count == 1); |
| 72 | assert(B::count == 1); |
| 73 | d.set_state(6); |
| 74 | assert(s2.get_deleter().state() == d.state()); |
| 75 | assert(s.get_deleter().state() == d.state()); |
| 76 | } |
| 77 | assert(A::count == 0); |
| 78 | assert(B::count == 0); |
| 79 | } |