Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
| 15 | int copy_ctor = 0; |
| 16 | int move_ctor = 0; |
| 17 | |
| 18 | class A |
| 19 | { |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 20 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | #else |
| 22 | #endif |
| 23 | |
| 24 | public: |
| 25 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 26 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | A(const A&) {++copy_ctor;} |
| 28 | A& operator=(const A&); |
| 29 | |
| 30 | A(A&&) {++move_ctor;} |
| 31 | A& operator=(A&&); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 32 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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;} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 38 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | |
| 40 | A() {} |
| 41 | }; |
| 42 | |
| 43 | A source() {return A();} |
| 44 | const A csource() {return A();} |
| 45 | |
| 46 | void test(A) {} |
| 47 | |
| 48 | int 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 | } |