move_copy.pass.cpp revision 73d21a4f0774d3fadab98e690619a359cfb160a3
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
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{
20#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
21#else
22#endif
23
24public:
25
26#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
27    A(const A&) {++copy_ctor;}
28    A& operator=(const A&);
29
30    A(A&&) {++move_ctor;}
31    A& operator=(A&&);
32#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
33    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;}
38#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
39
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}
72