1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
12 #include <type_traits>
15 #include "test_macros.h"
19 move_only(const move_only
&);
20 move_only
& operator=(const move_only
&);
22 move_only(move_only
&&) {}
23 move_only
& operator=(move_only
&&) {return *this;}
28 move_only
source() {return move_only();}
29 const move_only
csource() {return move_only();}
31 void test(move_only
) {}
36 template <class QualInt
>
37 QualInt
get() TEST_NOEXCEPT
{ return static_cast<QualInt
>(x
); }
45 A(const A
&) {++copy_ctor
;}
47 A
& operator=(const A
&) = delete;
51 constexpr bool test_constexpr_move() {
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;
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
76 assert(copy_ctor
== 0);
77 assert(move_ctor
== 0);
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);
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
100 #if TEST_STD_VER > 11
102 constexpr int y
= 42;
103 static_assert(std::move(y
) == 42, "");
104 static_assert(test_constexpr_move(), "");
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, "");