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 "MoveOnly.h"
26 #include "test_allocator.h"
27 
28 template <class T>
29 struct some_alloc
30 {
31     typedef T value_type;
32 
some_allocsome_alloc33     some_alloc() {}
34     some_alloc(const some_alloc&);
deallocatesome_alloc35     void deallocate(void*, unsigned) {}
36 
37     typedef std::true_type propagate_on_container_swap;
38 };
39 
40 template <class T>
41 struct some_alloc2
42 {
43     typedef T value_type;
44 
some_alloc2some_alloc245     some_alloc2() {}
46     some_alloc2(const some_alloc2&);
deallocatesome_alloc247     void deallocate(void*, unsigned) {}
48 
49     typedef std::false_type propagate_on_container_swap;
50     typedef std::true_type is_always_equal;
51 };
52 
main()53 int main()
54 {
55 #if __has_feature(cxx_noexcept)
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;
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
78         static_assert(!noexcept(swap(c1, c2)), "");
79 #endif
80     }
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
89 
90 #endif
91 }
92