Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +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 | // <vector> |
| 11 | |
| 12 | // void swap(vector& 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>::propagate_on_container_swap::value || |
| 18 | // allocator_traits<Allocator>::is_always_equal::value); |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 19 | |
| 20 | // This tests a conforming extension |
| 21 | |
| 22 | #include <vector> |
| 23 | #include <cassert> |
| 24 | |
Marshall Clow | 1b92188 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 25 | #include "test_allocator.h" |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 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 | |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame^] | 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 | |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 52 | int main() |
| 53 | { |
| 54 | #if __has_feature(cxx_noexcept) |
| 55 | { |
| 56 | typedef std::vector<bool> C; |
| 57 | C c1, c2; |
| 58 | static_assert(noexcept(swap(c1, c2)), ""); |
| 59 | } |
| 60 | { |
| 61 | typedef std::vector<bool, test_allocator<bool>> C; |
| 62 | C c1, c2; |
| 63 | static_assert(noexcept(swap(c1, c2)), ""); |
| 64 | } |
| 65 | { |
| 66 | typedef std::vector<bool, other_allocator<bool>> C; |
| 67 | C c1, c2; |
| 68 | static_assert(noexcept(swap(c1, c2)), ""); |
| 69 | } |
| 70 | { |
| 71 | typedef std::vector<bool, some_alloc<bool>> C; |
| 72 | C c1, c2; |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame^] | 73 | #if TEST_STD_VER >= 14 |
| 74 | // In c++14, if POCS is set, swapping the allocator is required not to throw |
| 75 | static_assert( noexcept(swap(c1, c2)), ""); |
| 76 | #else |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 77 | static_assert(!noexcept(swap(c1, c2)), ""); |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame^] | 78 | #endif |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 79 | } |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame^] | 80 | #if TEST_STD_VER >= 14 |
| 81 | { |
| 82 | typedef std::vector<bool, some_alloc2<bool>> C; |
| 83 | C c1, c2; |
| 84 | // if the allocators are always equal, then the swap can be noexcept |
| 85 | static_assert( noexcept(swap(c1, c2)), ""); |
| 86 | } |
| 87 | #endif |
| 88 | |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 89 | #endif |
| 90 | } |