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 // UNSUPPORTED: c++98, c++03 11 12 // <map> 13 14 // void swap(map& c) 15 // noexcept(!allocator_type::propagate_on_container_swap::value || 16 // __is_nothrow_swappable<allocator_type>::value); 17 // 18 // In C++17, the standard says that swap shall have: 19 // noexcept(allocator_traits<Allocator>::is_always_equal::value && 20 // noexcept(swap(declval<Compare&>(), declval<Compare&>()))); 21 22 // This tests a conforming extension 23 24 #include <map> 25 #include <cassert> 26 27 #include "test_macros.h" 28 #include "MoveOnly.h" 29 #include "test_allocator.h" 30 31 template <class T> 32 struct some_comp 33 { 34 typedef T value_type; 35 36 some_comp() {} 37 some_comp(const some_comp&) {} 38 void deallocate(void*, unsigned) {} 39 40 typedef std::true_type propagate_on_container_swap; 41 }; 42 43 template <class T> 44 struct some_comp2 45 { 46 typedef T value_type; 47 48 some_comp2() {} 49 some_comp2(const some_comp2&) {} 50 void deallocate(void*, unsigned) {} 51 typedef std::true_type propagate_on_container_swap; 52 }; 53 54 #if TEST_STD_VER >= 14 55 template <typename T> 56 void swap(some_comp2<T>&, some_comp2<T>&) noexcept {} 57 #endif 58 59 template <class T> 60 struct some_alloc 61 { 62 typedef T value_type; 63 64 some_alloc() {} 65 some_alloc(const some_alloc&); 66 void deallocate(void*, unsigned) {} 67 68 typedef std::true_type propagate_on_container_swap; 69 }; 70 71 template <class T> 72 struct some_alloc2 73 { 74 typedef T value_type; 75 76 some_alloc2() {} 77 some_alloc2(const some_alloc2&); 78 void deallocate(void*, unsigned) {} 79 80 typedef std::false_type propagate_on_container_swap; 81 typedef std::true_type is_always_equal; 82 }; 83 84 template <class T> 85 struct some_alloc3 86 { 87 typedef T value_type; 88 89 some_alloc3() {} 90 some_alloc3(const some_alloc3&); 91 void deallocate(void*, unsigned) {} 92 93 typedef std::false_type propagate_on_container_swap; 94 typedef std::false_type is_always_equal; 95 }; 96 97 int main() 98 { 99 typedef std::pair<const MoveOnly, MoveOnly> V; 100 { 101 typedef std::map<MoveOnly, MoveOnly> C; 102 C c1, c2; 103 static_assert(noexcept(swap(c1, c2)), ""); 104 } 105 { 106 typedef std::map<MoveOnly, MoveOnly, std::less<MoveOnly>, test_allocator<V>> C; 107 C c1, c2; 108 static_assert(noexcept(swap(c1, c2)), ""); 109 } 110 { 111 typedef std::map<MoveOnly, MoveOnly, std::less<MoveOnly>, other_allocator<V>> C; 112 C c1, c2; 113 static_assert(noexcept(swap(c1, c2)), ""); 114 } 115 { 116 typedef std::map<MoveOnly, MoveOnly, some_comp<MoveOnly>> C; 117 C c1, c2; 118 static_assert(!noexcept(swap(c1, c2)), ""); 119 } 120 121 #if TEST_STD_VER >= 14 122 { // POCS allocator, throwable swap for comp 123 typedef std::map<MoveOnly, MoveOnly, some_comp <MoveOnly>, some_alloc <V>> C; 124 C c1, c2; 125 static_assert(!noexcept(swap(c1, c2)), ""); 126 } 127 { // always equal allocator, throwable swap for comp 128 typedef std::map<MoveOnly, MoveOnly, some_comp <MoveOnly>, some_alloc2<V>> C; 129 C c1, c2; 130 static_assert(!noexcept(swap(c1, c2)), ""); 131 } 132 { // POCS allocator, nothrow swap for comp 133 typedef std::map<MoveOnly, MoveOnly, some_comp2<MoveOnly>, some_alloc <V>> C; 134 C c1, c2; 135 static_assert( noexcept(swap(c1, c2)), ""); 136 } 137 { // always equal allocator, nothrow swap for comp 138 typedef std::map<MoveOnly, MoveOnly, some_comp2<MoveOnly>, some_alloc2<V>> C; 139 C c1, c2; 140 static_assert( noexcept(swap(c1, c2)), ""); 141 } 142 143 { // NOT always equal allocator, nothrow swap for comp 144 typedef std::map<MoveOnly, MoveOnly, some_comp2<MoveOnly>, some_alloc3<V>> C; 145 C c1, c2; 146 static_assert( noexcept(swap(c1, c2)), ""); 147 } 148 #endif 149 150 } 151