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 // <vector> 13 14 // void swap(vector& 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>::propagate_on_container_swap::value || 20 // allocator_traits<Allocator>::is_always_equal::value); 21 22 // This tests a conforming extension 23 24 #include <vector> 25 #include <utility> 26 #include <cassert> 27 28 #include "test_macros.h" 29 #include "test_allocator.h" 30 31 template <class T> 32 struct some_alloc 33 { 34 typedef T value_type; 35 36 some_alloc() {} 37 some_alloc(const some_alloc&); 38 void deallocate(void*, unsigned) {} 39 40 typedef std::true_type propagate_on_container_swap; 41 }; 42 43 template <class T> 44 struct some_alloc2 45 { 46 typedef T value_type; 47 48 some_alloc2() {} 49 some_alloc2(const some_alloc2&); 50 void deallocate(void*, unsigned) {} 51 52 typedef std::false_type propagate_on_container_swap; 53 typedef std::true_type is_always_equal; 54 }; 55 56 int main() 57 { 58 #if defined(_LIBCPP_VERSION) 59 { 60 typedef std::vector<bool> C; 61 static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), ""); 62 } 63 { 64 typedef std::vector<bool, test_allocator<bool>> C; 65 static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), ""); 66 } 67 { 68 typedef std::vector<bool, other_allocator<bool>> C; 69 static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), ""); 70 } 71 #endif // _LIBCPP_VERSION 72 { 73 #if TEST_STD_VER >= 14 74 #if defined(_LIBCPP_VERSION) 75 // In c++14, if POCS is set, swapping the allocator is required not to throw 76 typedef std::vector<bool, some_alloc<bool>> C; 77 static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), ""); 78 #endif // _LIBCPP_VERSION 79 #else 80 typedef std::vector<bool, some_alloc<bool>> C; 81 static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), ""); 82 #endif 83 } 84 #if TEST_STD_VER >= 14 85 #if defined(_LIBCPP_VERSION) 86 { 87 typedef std::vector<bool, some_alloc2<bool>> C; 88 // if the allocators are always equal, then the swap can be noexcept 89 static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), ""); 90 } 91 #endif // _LIBCPP_VERSION 92 #endif 93 } 94