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 // <string> 11 12 // void swap(basic_string& c) 13 // noexcept(!allocator_type::propagate_on_container_swap::value || 14 // __is_nothrow_swappable<allocator_type>::value); 15 // 16 // In C++17, the standard says that swap shall have: 17 // noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value || 18 // allocator_traits<Allocator>::is_always_equal::value); 19 20 // This tests a conforming extension 21 22 #include <string> 23 #include <cassert> 24 25 #include "test_allocator.h" 26 27 template <class T> 28 struct some_alloc 29 { 30 typedef T value_type; 31 32 some_alloc() {} 33 some_alloc(const some_alloc&); 34 void deallocate(void*, unsigned) {} 35 36 typedef std::true_type propagate_on_container_swap; 37 }; 38 39 template <class T> 40 struct some_alloc2 41 { 42 typedef T value_type; 43 44 some_alloc2() {} 45 some_alloc2(const some_alloc2&); 46 void deallocate(void*, unsigned) {} 47 48 typedef std::false_type propagate_on_container_swap; 49 typedef std::true_type is_always_equal; 50 }; 51 52 int main() 53 { 54 #if __has_feature(cxx_noexcept) 55 { 56 typedef std::string C; 57 C c1, c2; 58 static_assert(noexcept(swap(c1, c2)), ""); 59 } 60 { 61 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C; 62 C c1, c2; 63 static_assert(noexcept(swap(c1, c2)), ""); 64 } 65 { 66 typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C; 67 C c1, c2; 68 #if TEST_STD_VER >= 14 69 // In c++14, if POCS is set, swapping the allocator is required not to throw 70 static_assert( noexcept(swap(c1, c2)), ""); 71 #else 72 static_assert(!noexcept(swap(c1, c2)), ""); 73 #endif 74 } 75 #if TEST_STD_VER >= 14 76 { 77 typedef std::basic_string<char, std::char_traits<char>, some_alloc2<char>> C; 78 C c1, c2; 79 // if the allocators are always equal, then the swap can be noexcept 80 static_assert( noexcept(swap(c1, c2)), ""); 81 } 82 #endif 83 84 #endif 85 } 86