80 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// UNSUPPORTED: c++03, c++11, c++1410 11// type_traits12 13// is_swappable_with14 15#include <type_traits>16#include <vector>17#include "test_macros.h"18 19namespace MyNS {20 21struct A {22 A(A const&) = delete;23 A& operator=(A const&) = delete;24};25 26struct B {27 B(B const&) = delete;28 B& operator=(B const&) = delete;29};30 31struct C {};32struct D {};33 34void swap(A&, A&) {}35 36void swap(A&, B&) {}37void swap(B&, A&) {}38 39void swap(A&, C&) {} // missing swap(C, A)40void swap(D&, C&) {}41 42struct M {};43 44void swap(M&&, M&&) {}45 46} // namespace MyNS47 48int main(int, char**)49{50 using namespace MyNS;51 {52 // Test that is_swappable_with doesn't apply an lvalue reference53 // to the type. Instead it is up to the user.54 static_assert(!std::is_swappable_with<int, int>::value, "");55 static_assert(std::is_swappable_with<int&, int&>::value, "");56 static_assert(std::is_swappable_with<M, M>::value, "");57 static_assert(std::is_swappable_with<A&, A&>::value, "");58 }59 {60 // test that heterogeneous swap is allowed only if both 'swap(A, B)' and61 // 'swap(B, A)' are valid.62 static_assert(std::is_swappable_with<A&, B&>::value, "");63 static_assert(!std::is_swappable_with<A&, C&>::value, "");64 static_assert(!std::is_swappable_with<D&, C&>::value, "");65 }66 {67 // test that cv void is guarded against as required.68 static_assert(!std::is_swappable_with_v<void, int>, "");69 static_assert(!std::is_swappable_with_v<int, void>, "");70 static_assert(!std::is_swappable_with_v<const void, const volatile void>, "");71 }72 {73 // test for presence of is_swappable_with_v74 static_assert(std::is_swappable_with_v<int&, int&>, "");75 static_assert(!std::is_swappable_with_v<D&, C&>, "");76 }77 78 return 0;79}80