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 
16 #include "test_macros.h"
17 // std::array is explicitly allowed to be initialized with A a = { init-list };.
18 // Disable the missing braces warning for this reason.
19 #include "disable_missing_braces_warning.h"
20 
21 struct NonSwappable {
NonSwappableNonSwappable22   NonSwappable() {}
23 private:
24   NonSwappable(NonSwappable const&);
25   NonSwappable& operator=(NonSwappable const&);
26 };
27 
28 template <class Tp>
29 decltype(swap(std::declval<Tp>(), std::declval<Tp>()))
30 can_swap_imp(int);
31 
32 template <class Tp>
33 std::false_type can_swap_imp(...);
34 
35 template <class Tp>
36 struct can_swap : std::is_same<decltype(can_swap_imp<Tp>(0)), void> {};
37 
main(int,char **)38 int main(int, char**)
39 {
40     {
41         typedef double T;
42         typedef std::array<T, 3> C;
43         C c1 = {1, 2, 3.5};
44         C c2 = {4, 5, 6.5};
45         swap(c1, c2);
46         assert(c1.size() == 3);
47         assert(c1[0] == 4);
48         assert(c1[1] == 5);
49         assert(c1[2] == 6.5);
50         assert(c2.size() == 3);
51         assert(c2[0] == 1);
52         assert(c2[1] == 2);
53         assert(c2[2] == 3.5);
54     }
55     {
56         typedef double T;
57         typedef std::array<T, 0> C;
58         C c1 = {};
59         C c2 = {};
60         swap(c1, c2);
61         assert(c1.size() == 0);
62         assert(c2.size() == 0);
63     }
64     {
65         typedef NonSwappable T;
66         typedef std::array<T, 0> C0;
67         static_assert(can_swap<C0&>::value, "");
68         C0 l = {};
69         C0 r = {};
70         swap(l, r);
71 #if TEST_STD_VER >= 11
72         static_assert(noexcept(swap(l, r)), "");
73 #endif
74     }
75 #if TEST_STD_VER >= 11
76     {
77         // NonSwappable is still considered swappable in C++03 because there
78         // is no access control SFINAE.
79         typedef NonSwappable T;
80         typedef std::array<T, 42> C1;
81         static_assert(!can_swap<C1&>::value, "");
82     }
83 #endif
84 
85   return 0;
86 }
87