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 | 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 | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 27 | |
| 28 | template <class T> |
| 29 | struct some_alloc |
| 30 | { |
| 31 | typedef T value_type; |
| 32 | |
| 33 | some_alloc() {} |
| 34 | some_alloc(const some_alloc&); |
| 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_alloc2 |
| 42 | { |
| 43 | typedef T value_type; |
| 44 | |
| 45 | some_alloc2() {} |
| 46 | some_alloc2(const some_alloc2&); |
| 47 | void deallocate(void*, unsigned) {} |
| 48 | |
| 49 | typedef std::false_type propagate_on_container_swap; |
| 50 | typedef std::true_type is_always_equal; |
| 51 | }; |
| 52 | |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 53 | int main() |
| 54 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 55 | #if __has_feature(cxx_noexcept) |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 56 | { |
| 57 | typedef std::vector<MoveOnly> C; |
| 58 | C c1, c2; |
| 59 | static_assert(noexcept(swap(c1, c2)), ""); |
| 60 | } |
| 61 | { |
| 62 | typedef std::vector<MoveOnly, test_allocator<MoveOnly>> C; |
| 63 | C c1, c2; |
| 64 | static_assert(noexcept(swap(c1, c2)), ""); |
| 65 | } |
| 66 | { |
| 67 | typedef std::vector<MoveOnly, other_allocator<MoveOnly>> C; |
| 68 | C c1, c2; |
| 69 | static_assert(noexcept(swap(c1, c2)), ""); |
| 70 | } |
| 71 | { |
| 72 | typedef std::vector<MoveOnly, some_alloc<MoveOnly>> C; |
| 73 | C c1, c2; |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 74 | #if TEST_STD_VER >= 14 |
| 75 | // In c++14, if POCS is set, swapping the allocator is required not to throw |
| 76 | static_assert( noexcept(swap(c1, c2)), ""); |
| 77 | #else |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 78 | static_assert(!noexcept(swap(c1, c2)), ""); |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 79 | #endif |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 80 | } |
Marshall Clow | 7d914d1 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 81 | #if TEST_STD_VER >= 14 |
| 82 | { |
| 83 | typedef std::vector<MoveOnly, some_alloc2<MoveOnly>> C; |
| 84 | C c1, c2; |
| 85 | // if the allocators are always equal, then the swap can be noexcept |
| 86 | static_assert( noexcept(swap(c1, c2)), ""); |
| 87 | } |
| 88 | #endif |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 89 | |
| 90 | #endif |
Howard Hinnant | d1d27a4 | 2011-06-03 19:40:40 +0000 | [diff] [blame] | 91 | } |