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 | // <array> |
10 | |
11 | // template <class T, size_t N> void swap(array<T,N>& x, array<T,N>& y); |
12 | |
13 | #include <array> |
14 | #include <cassert> |
15 | #include <utility> |
16 | |
17 | #include "test_macros.h" |
18 | |
19 | struct NonSwappable { |
20 | TEST_CONSTEXPR NonSwappable() { } |
21 | private: |
22 | NonSwappable(NonSwappable const&); |
23 | NonSwappable& operator=(NonSwappable const&); |
24 | }; |
25 | |
26 | template <class Tp> |
27 | decltype(swap(std::declval<Tp>(), std::declval<Tp>())) |
28 | can_swap_imp(int); |
29 | |
30 | template <class Tp> |
31 | std::false_type can_swap_imp(...); |
32 | |
33 | template <class Tp> |
34 | struct can_swap : std::is_same<decltype(can_swap_imp<Tp>(0)), void> { }; |
35 | |
36 | TEST_CONSTEXPR_CXX20 bool tests() |
37 | { |
38 | { |
39 | typedef double T; |
40 | typedef std::array<T, 3> C; |
41 | C c1 = {1, 2, 3.5}; |
42 | C c2 = {4, 5, 6.5}; |
43 | swap(one&: c1, two&: c2); |
44 | assert(c1.size() == 3); |
45 | assert(c1[0] == 4); |
46 | assert(c1[1] == 5); |
47 | assert(c1[2] == 6.5); |
48 | assert(c2.size() == 3); |
49 | assert(c2[0] == 1); |
50 | assert(c2[1] == 2); |
51 | assert(c2[2] == 3.5); |
52 | } |
53 | { |
54 | typedef double T; |
55 | typedef std::array<T, 0> C; |
56 | C c1 = {}; |
57 | C c2 = {}; |
58 | swap(one&: c1, two&: c2); |
59 | assert(c1.size() == 0); |
60 | assert(c2.size() == 0); |
61 | } |
62 | { |
63 | typedef NonSwappable T; |
64 | typedef std::array<T, 0> C0; |
65 | static_assert(can_swap<C0&>::value, "" ); |
66 | C0 l = {}; |
67 | C0 r = {}; |
68 | swap(one&: l, two&: r); |
69 | #if TEST_STD_VER >= 11 |
70 | static_assert(noexcept(swap(l, r)), "" ); |
71 | #endif |
72 | } |
73 | #if TEST_STD_VER >= 11 |
74 | { |
75 | // NonSwappable is still considered swappable in C++03 because there |
76 | // is no access control SFINAE. |
77 | typedef NonSwappable T; |
78 | typedef std::array<T, 42> C1; |
79 | static_assert(!can_swap<C1&>::value, "" ); |
80 | } |
81 | #endif |
82 | |
83 | return true; |
84 | } |
85 | |
86 | int main(int, char**) |
87 | { |
88 | tests(); |
89 | #if TEST_STD_VER >= 20 |
90 | static_assert(tests(), "" ); |
91 | #endif |
92 | return 0; |
93 | } |
94 | |