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 //===----------------------------------------------------------------------===//
8
9 // <utility>
10
11 // template<class T>
12 // requires MoveAssignable<T> && MoveConstructible<T>
13 // void
14 // swap(T& a, T& b);
15
16 #include <utility>
17 #include <cassert>
18 #include <memory>
19
20 #include "test_macros.h"
21
22 #if TEST_STD_VER >= 11
23 struct CopyOnly {
CopyOnlyCopyOnly24 CopyOnly() {}
CopyOnlyCopyOnly25 CopyOnly(CopyOnly const&) noexcept {}
operator =CopyOnly26 CopyOnly& operator=(CopyOnly const&) { return *this; }
27 };
28
29 struct MoveOnly {
MoveOnlyMoveOnly30 MoveOnly() {}
MoveOnlyMoveOnly31 MoveOnly(MoveOnly&&) {}
operator =MoveOnly32 MoveOnly& operator=(MoveOnly&&) noexcept { return *this; }
33 };
34
35 struct NoexceptMoveOnly {
NoexceptMoveOnlyNoexceptMoveOnly36 NoexceptMoveOnly() {}
NoexceptMoveOnlyNoexceptMoveOnly37 NoexceptMoveOnly(NoexceptMoveOnly&&) noexcept {}
operator =NoexceptMoveOnly38 NoexceptMoveOnly& operator=(NoexceptMoveOnly&&) noexcept { return *this; }
39 };
40
41 struct NotMoveConstructible {
operator =NotMoveConstructible42 NotMoveConstructible& operator=(NotMoveConstructible&&) { return *this; }
43 private:
44 NotMoveConstructible(NotMoveConstructible&&);
45 };
46
47 struct NotMoveAssignable {
48 NotMoveAssignable(NotMoveAssignable&&);
49 private:
50 NotMoveAssignable& operator=(NotMoveAssignable&&);
51 };
52
53 template <class Tp>
54 auto can_swap_test(int) -> decltype(std::swap(std::declval<Tp>(), std::declval<Tp>()));
55
56 template <class Tp>
57 auto can_swap_test(...) -> std::false_type;
58
59 template <class Tp>
can_swap()60 constexpr bool can_swap() {
61 return std::is_same<decltype(can_swap_test<Tp>(0)), void>::value;
62 }
63 #endif
64
65 #if TEST_STD_VER > 17
test_swap_constexpr()66 constexpr bool test_swap_constexpr()
67 {
68 int i = 1;
69 int j = 2;
70 std::swap(i, j);
71 return i == 2 && j == 1;
72 }
73 #endif // TEST_STD_VER > 17
74
main(int,char **)75 int main(int, char**)
76 {
77
78 {
79 int i = 1;
80 int j = 2;
81 std::swap(i, j);
82 assert(i == 2);
83 assert(j == 1);
84 }
85 #if TEST_STD_VER >= 11
86 {
87
88 std::unique_ptr<int> i(new int(1));
89 std::unique_ptr<int> j(new int(2));
90 std::swap(i, j);
91 assert(*i == 2);
92 assert(*j == 1);
93
94 }
95 {
96 // test that the swap
97 static_assert(can_swap<CopyOnly&>(), "");
98 static_assert(can_swap<MoveOnly&>(), "");
99 static_assert(can_swap<NoexceptMoveOnly&>(), "");
100
101 static_assert(!can_swap<NotMoveConstructible&>(), "");
102 static_assert(!can_swap<NotMoveAssignable&>(), "");
103
104 CopyOnly c;
105 MoveOnly m;
106 NoexceptMoveOnly nm;
107 static_assert(!noexcept(std::swap(c, c)), "");
108 static_assert(!noexcept(std::swap(m, m)), "");
109 static_assert(noexcept(std::swap(nm, nm)), "");
110 }
111 #endif
112
113 #if TEST_STD_VER > 17
114 static_assert(test_swap_constexpr());
115 #endif // TEST_STD_VER > 17
116
117 return 0;
118 }
119