blob: c4feb14672af272b07eaf218b87b34bcbcada3de [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// test move
11
12#include <utility>
13#include <cassert>
14
15int copy_ctor = 0;
16int move_ctor = 0;
17
18class A
19{
Howard Hinnant73d21a42010-09-04 23:28:19 +000020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#else
22#endif
23
24public:
25
Howard Hinnant73d21a42010-09-04 23:28:19 +000026#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000027 A(const A&) {++copy_ctor;}
28 A& operator=(const A&);
29
30 A(A&&) {++move_ctor;}
31 A& operator=(A&&);
Howard Hinnant73d21a42010-09-04 23:28:19 +000032#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033 A(const A&) {++copy_ctor;}
34 A& operator=(A&);
35
36 operator std::__rv<A> () {return std::__rv<A>(*this);}
37 A(std::__rv<A>) {++move_ctor;}
Howard Hinnant73d21a42010-09-04 23:28:19 +000038#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000039
40 A() {}
41};
42
43A source() {return A();}
44const A csource() {return A();}
45
46void test(A) {}
47
48int main()
49{
50 A a;
51 const A ca = A();
52
53 assert(copy_ctor == 0);
54 assert(move_ctor == 0);
55
56 A a2 = a;
57 assert(copy_ctor == 1);
58 assert(move_ctor == 0);
59
60 A a3 = std::move(a);
61 assert(copy_ctor == 1);
62 assert(move_ctor == 1);
63
64 A a4 = ca;
65 assert(copy_ctor == 2);
66 assert(move_ctor == 1);
67
68 A a5 = std::move(ca);
69 assert(copy_ctor == 3);
70 assert(move_ctor == 1);
71}