blob: e0d7c891c80f68f278e83c66e6bd8b495bd83d80 [file] [log] [blame]
Howard Hinnantc52f43e2010-08-22 00:59:46 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc52f43e2010-08-22 00:59:46 +00007//
8//===----------------------------------------------------------------------===//
9
10// <memory>
11
12// unique_ptr
13
Dan Albert1d4a1ed2016-05-25 22:36:09 -070014// Test unique_ptr move ctor
Howard Hinnantc52f43e2010-08-22 00:59:46 +000015
16#include <memory>
Dan Albert1d4a1ed2016-05-25 22:36:09 -070017#include <cassert>
Howard Hinnantc52f43e2010-08-22 00:59:46 +000018
Dan Albert1d4a1ed2016-05-25 22:36:09 -070019// test move ctor. Can't copy from const lvalue
Howard Hinnantc52f43e2010-08-22 00:59:46 +000020
Dan Albert1d4a1ed2016-05-25 22:36:09 -070021struct A
22{
23 static int count;
24 A() {++count;}
25 A(const A&) {++count;}
26 ~A() {--count;}
Howard Hinnantc52f43e2010-08-22 00:59:46 +000027};
28
Dan Albert1d4a1ed2016-05-25 22:36:09 -070029int A::count = 0;
30
31class Deleter
32{
33 int state_;
34
35public:
36
37 Deleter() : state_(5) {}
38
39 int state() const {return state_;}
40
41 void operator()(A* p) {delete p;}
42};
43
Howard Hinnantc52f43e2010-08-22 00:59:46 +000044int main()
45{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070046 {
47 const std::unique_ptr<A, Deleter> s(new A);
48 A* p = s.get();
49 std::unique_ptr<A, Deleter> s2;
50 s2 = s;
51 assert(s2.get() == p);
52 assert(s.get() == 0);
53 assert(A::count == 1);
54 }
55 assert(A::count == 0);
Howard Hinnantc52f43e2010-08-22 00:59:46 +000056}