92 lines · cpp
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-exception6//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 19struct NonSwappable {20 TEST_CONSTEXPR NonSwappable() {}21 22private:23 NonSwappable(NonSwappable const&);24 NonSwappable& operator=(NonSwappable const&);25};26 27template <class Tp>28decltype(swap(std::declval<Tp>(), std::declval<Tp>())) can_swap_imp(int);29 30template <class Tp>31std::false_type can_swap_imp(...);32 33template <class Tp>34struct can_swap : std::is_same<decltype(can_swap_imp<Tp>(0)), void> {};35 36TEST_CONSTEXPR_CXX20 bool tests() {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 >= 1169 static_assert(noexcept(swap(l, r)), "");70#endif71 }72#if TEST_STD_VER >= 1173 {74 // NonSwappable is still considered swappable in C++03 because there75 // 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#endif81 82 return true;83}84 85int main(int, char**) {86 tests();87#if TEST_STD_VER >= 2088 static_assert(tests(), "");89#endif90 return 0;91}92