Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <utility> |
| 10 | |
Howard Hinnant | 94b2dd0 | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 11 | // template<class T> |
| 12 | // requires MoveAssignable<T> && MoveConstructible<T> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | // void |
| 14 | // swap(T& a, T& b); |
| 15 | |
| 16 | #include <utility> |
| 17 | #include <cassert> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 18 | #include <memory> |
Eric Fiselier | f07dd8d | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 19 | |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | #if TEST_STD_VER >= 11 |
| 23 | struct CopyOnly { |
| 24 | CopyOnly() {} |
| 25 | CopyOnly(CopyOnly const&) noexcept {} |
| 26 | CopyOnly& operator=(CopyOnly const&) { return *this; } |
| 27 | }; |
| 28 | |
| 29 | struct MoveOnly { |
| 30 | MoveOnly() {} |
| 31 | MoveOnly(MoveOnly&&) {} |
| 32 | MoveOnly& operator=(MoveOnly&&) noexcept { return *this; } |
| 33 | }; |
| 34 | |
| 35 | struct NoexceptMoveOnly { |
| 36 | NoexceptMoveOnly() {} |
| 37 | NoexceptMoveOnly(NoexceptMoveOnly&&) noexcept {} |
| 38 | NoexceptMoveOnly& operator=(NoexceptMoveOnly&&) noexcept { return *this; } |
| 39 | }; |
| 40 | |
| 41 | struct NotMoveConstructible { |
| 42 | 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> |
| 60 | constexpr bool can_swap() { |
| 61 | return std::is_same<decltype(can_swap_test<Tp>(0)), void>::value; |
| 62 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Zoe Carver | 28e0187 | 2019-07-05 20:13:34 +0000 | [diff] [blame] | 65 | #if TEST_STD_VER > 17 |
| 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 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 75 | int main(int, char**) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 76 | { |
Eric Fiselier | f07dd8d | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 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 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 111 | #endif |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 112 | |
Zoe Carver | 28e0187 | 2019-07-05 20:13:34 +0000 | [diff] [blame] | 113 | #if TEST_STD_VER > 17 |
| 114 | static_assert(test_swap_constexpr()); |
| 115 | #endif // TEST_STD_VER > 17 |
| 116 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 117 | return 0; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 118 | } |