[memprof] Move YAML traits to MemProf.h (NFC) (#118668)
[llvm-project.git] / libcxx / test / std / utilities / utility / forward / move.pass.cpp
blobe04aa138b5725892a7d391a181538ff7b64e0438
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // test move
11 #include <utility>
12 #include <type_traits>
13 #include <cassert>
15 #include "test_macros.h"
17 class move_only
19 move_only(const move_only&);
20 move_only& operator=(const move_only&);
21 public:
22 move_only(move_only&&) {}
23 move_only& operator=(move_only&&) {return *this;}
25 move_only() {}
28 move_only source() {return move_only();}
29 const move_only csource() {return move_only();}
31 void test(move_only) {}
33 int x = 42;
34 const int& cx = x;
36 template <class QualInt>
37 QualInt get() TEST_NOEXCEPT { return static_cast<QualInt>(x); }
40 int copy_ctor = 0;
41 int move_ctor = 0;
43 struct A {
44 A() {}
45 A(const A&) {++copy_ctor;}
46 A(A&&) {++move_ctor;}
47 A& operator=(const A&) = delete;
50 #if TEST_STD_VER > 11
51 constexpr bool test_constexpr_move() {
52 int y = 42;
53 const int cy = y;
54 return std::move(y) == 42
55 && std::move(cy) == 42
56 && std::move(static_cast<int&&>(y)) == 42
57 && std::move(static_cast<int const&&>(y)) == 42;
59 #endif
60 int main(int, char**)
62 { // Test return type and noexcept.
63 static_assert(std::is_same<decltype(std::move(x)), int&&>::value, "");
64 ASSERT_NOEXCEPT(std::move(x));
65 static_assert(std::is_same<decltype(std::move(cx)), const int&&>::value, "");
66 ASSERT_NOEXCEPT(std::move(cx));
67 static_assert(std::is_same<decltype(std::move(42)), int&&>::value, "");
68 ASSERT_NOEXCEPT(std::move(42));
69 static_assert(std::is_same<decltype(std::move(get<const int&&>())), const int&&>::value, "");
70 ASSERT_NOEXCEPT(std::move(get<int const&&>()));
72 { // test copy and move semantics
73 A a;
74 const A ca = A();
76 assert(copy_ctor == 0);
77 assert(move_ctor == 0);
79 A a2 = a; (void)a2;
80 assert(copy_ctor == 1);
81 assert(move_ctor == 0);
83 A a3 = std::move(a); (void)a3;
84 assert(copy_ctor == 1);
85 assert(move_ctor == 1);
87 A a4 = ca; (void)a4;
88 assert(copy_ctor == 2);
89 assert(move_ctor == 1);
91 A a5 = std::move(ca); (void)a5;
92 assert(copy_ctor == 3);
93 assert(move_ctor == 1);
95 { // test on a move only type
96 move_only mo;
97 test(std::move(mo));
98 test(source());
100 #if TEST_STD_VER > 11
102 constexpr int y = 42;
103 static_assert(std::move(y) == 42, "");
104 static_assert(test_constexpr_move(), "");
106 #endif
107 #if TEST_STD_VER == 11 && defined(_LIBCPP_VERSION)
108 // Test that std::forward is constexpr in C++11. This is an extension
109 // provided by both libc++ and libstdc++.
111 constexpr int y = 42;
112 static_assert(std::move(y) == 42, "");
114 #endif
116 return 0;