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);
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 <vector>
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 
some_allocsome_alloc32     some_alloc() {}
33     some_alloc(const some_alloc&);
deallocatesome_alloc34     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 
some_alloc2some_alloc244     some_alloc2() {}
45     some_alloc2(const some_alloc2&);
deallocatesome_alloc246     void deallocate(void*, unsigned) {}
47 
48     typedef std::false_type propagate_on_container_swap;
49     typedef std::true_type is_always_equal;
50 };
51 
main()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;
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
77         static_assert(!noexcept(swap(c1, c2)), "");
78 #endif
79     }
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 
89 #endif
90 }
91