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