xref: /llvm-project/libcxx/test/std/containers/sequences/deque/deque.special/swap_noexcept.pass.cpp (revision fb855eb941b6d740cc6560297d0b4d3201dcaf9f)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <deque>
12 
13 // void swap(deque& c)
14 //     noexcept(!allocator_type::propagate_on_container_swap::value ||
15 //              __is_nothrow_swappable<allocator_type>::value);
16 //
17 //  In C++17, the standard says that swap shall have:
18 //     noexcept(allocator_traits<Allocator>::is_always_equal::value);
19 
20 // This tests a conforming extension
21 
22 #include <deque>
23 #include <utility>
24 #include <cassert>
25 
26 #include "test_macros.h"
27 #include "MoveOnly.h"
28 #include "test_allocator.h"
29 
30 template <class T>
31 struct some_alloc
32 {
33     typedef T value_type;
34 
some_allocsome_alloc35     some_alloc() {}
36     some_alloc(const some_alloc&);
37     void allocate(std::size_t);
deallocatesome_alloc38     void deallocate(void*, unsigned) {}
39 
40     typedef std::true_type propagate_on_container_swap;
41 };
42 
43 template <class T>
44 struct some_alloc2
45 {
46     typedef T value_type;
47 
some_alloc2some_alloc248     some_alloc2() {}
49     some_alloc2(const some_alloc2&);
50     void allocate(std::size_t);
deallocatesome_alloc251     void deallocate(void*, unsigned) {}
52 
53     typedef std::false_type propagate_on_container_swap;
54     typedef std::true_type is_always_equal;
55 };
56 
main(int,char **)57 int main(int, char**)
58 {
59     {
60         typedef std::deque<MoveOnly> C;
61         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
62     }
63 #if defined(_LIBCPP_VERSION)
64     {
65         typedef std::deque<MoveOnly, test_allocator<MoveOnly>> C;
66         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
67     }
68     {
69         typedef std::deque<MoveOnly, other_allocator<MoveOnly>> C;
70         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
71     }
72 #endif // _LIBCPP_VERSION
73     {
74         typedef std::deque<MoveOnly, some_alloc<MoveOnly>> C;
75 #if TEST_STD_VER >= 14
76     //  In C++14, if POCS is set, swapping the allocator is required not to throw
77         static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
78 #else
79         static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
80 #endif
81     }
82 #if TEST_STD_VER >= 14
83     {
84         typedef std::deque<MoveOnly, some_alloc2<MoveOnly>> C;
85     //  if the allocators are always equal, then the swap can be noexcept
86         static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
87     }
88 #endif
89 
90 
91   return 0;
92 }
93