Howard Hinnant | b2e2a8f | 2011-06-04 15:22:34 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <set> |
| 11 | |
| 12 | // void swap(set& c) |
| 13 | // noexcept(!allocator_type::propagate_on_container_swap::value || |
| 14 | // __is_nothrow_swappable<allocator_type>::value); |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 15 | // |
| 16 | // In C++17, the standard says that swap shall have: |
| 17 | // noexcept(allocator_traits<Allocator>::is_always_equal::value && |
| 18 | // noexcept(swap(declval<Compare&>(), declval<Compare&>()))); |
Howard Hinnant | b2e2a8f | 2011-06-04 15:22:34 +0000 | [diff] [blame] | 19 | |
| 20 | // This tests a conforming extension |
| 21 | |
| 22 | #include <set> |
| 23 | #include <cassert> |
| 24 | |
Marshall Clow | df00d5e | 2015-01-28 21:22:53 +0000 | [diff] [blame] | 25 | #include "MoveOnly.h" |
Marshall Clow | 1b92188 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 26 | #include "test_allocator.h" |
Howard Hinnant | b2e2a8f | 2011-06-04 15:22:34 +0000 | [diff] [blame] | 27 | |
| 28 | template <class T> |
| 29 | struct some_comp |
| 30 | { |
| 31 | typedef T value_type; |
| 32 | |
| 33 | some_comp() {} |
| 34 | some_comp(const some_comp&) {} |
| 35 | void deallocate(void*, unsigned) {} |
| 36 | |
| 37 | typedef std::true_type propagate_on_container_swap; |
| 38 | }; |
| 39 | |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 40 | template <class T> |
| 41 | struct some_comp2 |
| 42 | { |
| 43 | typedef T value_type; |
| 44 | |
| 45 | some_comp2() {} |
| 46 | some_comp2(const some_comp2&) {} |
| 47 | void deallocate(void*, unsigned) {} |
| 48 | typedef std::true_type propagate_on_container_swap; |
| 49 | }; |
| 50 | |
| 51 | #if TEST_STD_VER >= 14 |
| 52 | template <typename T> |
| 53 | void swap(some_comp2<T>&, some_comp2<T>&) noexcept {} |
| 54 | #endif |
| 55 | |
| 56 | template <class T> |
| 57 | struct some_alloc |
| 58 | { |
| 59 | typedef T value_type; |
| 60 | |
| 61 | some_alloc() {} |
| 62 | some_alloc(const some_alloc&); |
| 63 | void deallocate(void*, unsigned) {} |
| 64 | |
| 65 | typedef std::true_type propagate_on_container_swap; |
| 66 | }; |
| 67 | |
| 68 | template <class T> |
| 69 | struct some_alloc2 |
| 70 | { |
| 71 | typedef T value_type; |
| 72 | |
| 73 | some_alloc2() {} |
| 74 | some_alloc2(const some_alloc2&); |
| 75 | void deallocate(void*, unsigned) {} |
| 76 | |
| 77 | typedef std::false_type propagate_on_container_swap; |
| 78 | typedef std::true_type is_always_equal; |
| 79 | }; |
| 80 | |
| 81 | template <class T> |
| 82 | struct some_alloc3 |
| 83 | { |
| 84 | typedef T value_type; |
| 85 | |
| 86 | some_alloc3() {} |
| 87 | some_alloc3(const some_alloc3&); |
| 88 | void deallocate(void*, unsigned) {} |
| 89 | |
| 90 | typedef std::false_type propagate_on_container_swap; |
| 91 | typedef std::false_type is_always_equal; |
| 92 | }; |
| 93 | |
Howard Hinnant | b2e2a8f | 2011-06-04 15:22:34 +0000 | [diff] [blame] | 94 | int main() |
| 95 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 96 | #if __has_feature(cxx_noexcept) |
Howard Hinnant | b2e2a8f | 2011-06-04 15:22:34 +0000 | [diff] [blame] | 97 | { |
| 98 | typedef std::set<MoveOnly> C; |
| 99 | C c1, c2; |
| 100 | static_assert(noexcept(swap(c1, c2)), ""); |
| 101 | } |
| 102 | { |
| 103 | typedef std::set<MoveOnly, std::less<MoveOnly>, test_allocator<MoveOnly>> C; |
| 104 | C c1, c2; |
| 105 | static_assert(noexcept(swap(c1, c2)), ""); |
| 106 | } |
| 107 | { |
| 108 | typedef std::set<MoveOnly, std::less<MoveOnly>, other_allocator<MoveOnly>> C; |
| 109 | C c1, c2; |
| 110 | static_assert(noexcept(swap(c1, c2)), ""); |
| 111 | } |
| 112 | { |
| 113 | typedef std::set<MoveOnly, some_comp<MoveOnly>> C; |
| 114 | C c1, c2; |
| 115 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 116 | } |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 117 | |
| 118 | #if TEST_STD_VER >= 14 |
| 119 | { // POCS allocator, throwable swap for comp |
| 120 | typedef std::set<MoveOnly, some_comp <MoveOnly>, some_alloc <MoveOnly>> C; |
| 121 | C c1, c2; |
| 122 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 123 | } |
| 124 | { // always equal allocator, throwable swap for comp |
| 125 | typedef std::set<MoveOnly, some_comp <MoveOnly>, some_alloc2<MoveOnly>> C; |
| 126 | C c1, c2; |
| 127 | static_assert(!noexcept(swap(c1, c2)), ""); |
| 128 | } |
| 129 | { // POCS allocator, nothrow swap for comp |
| 130 | typedef std::set<MoveOnly, some_comp2<MoveOnly>, some_alloc <MoveOnly>> C; |
| 131 | C c1, c2; |
| 132 | static_assert( noexcept(swap(c1, c2)), ""); |
| 133 | } |
| 134 | { // always equal allocator, nothrow swap for comp |
| 135 | typedef std::set<MoveOnly, some_comp2<MoveOnly>, some_alloc2<MoveOnly>> C; |
| 136 | C c1, c2; |
| 137 | static_assert( noexcept(swap(c1, c2)), ""); |
| 138 | } |
| 139 | |
| 140 | { // NOT always equal allocator, nothrow swap for comp |
| 141 | typedef std::set<MoveOnly, some_comp2<MoveOnly>, some_alloc3<MoveOnly>> C; |
| 142 | C c1, c2; |
| 143 | static_assert( noexcept(swap(c1, c2)), ""); |
| 144 | } |
| 145 | #endif |
| 146 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 147 | #endif |
Howard Hinnant | b2e2a8f | 2011-06-04 15:22:34 +0000 | [diff] [blame] | 148 | } |