1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11
12 // type_traits
13
14 // is_swappable_with
15
16 #include <type_traits>
17 #include <vector>
18 #include "test_macros.h"
19
20 namespace MyNS {
21
22 struct A {
23 A(A const&) = delete;
24 A& operator=(A const&) = delete;
25 };
26
27 struct B {
28 B(B const&) = delete;
29 B& operator=(B const&) = delete;
30 };
31
32 struct C {};
33 struct D {};
34
swap(A &,A &)35 void swap(A&, A&) {}
36
swap(A &,B &)37 void swap(A&, B&) {}
swap(B &,A &)38 void swap(B&, A&) {}
39
swap(A &,C &)40 void swap(A&, C&) {} // missing swap(C, A)
swap(D &,C &)41 void swap(D&, C&) {}
42
43 struct M {};
44
swap(M &&,M &&)45 void swap(M&&, M&&) {}
46
47 } // namespace MyNS
48
main()49 int main()
50 {
51 using namespace MyNS;
52 {
53 // Test that is_swappable_with doesn't apply an lvalue reference
54 // to the type. Instead it is up to the user.
55 static_assert(!std::is_swappable_with<int, int>::value, "");
56 static_assert(std::is_swappable_with<int&, int&>::value, "");
57 static_assert(std::is_swappable_with<M, M>::value, "");
58 static_assert(std::is_swappable_with<A&, A&>::value, "");
59 }
60 {
61 // test that heterogeneous swap is allowed only if both 'swap(A, B)' and
62 // 'swap(B, A)' are valid.
63 static_assert(std::is_swappable_with<A&, B&>::value, "");
64 static_assert(!std::is_swappable_with<A&, C&>::value, "");
65 static_assert(!std::is_swappable_with<D&, C&>::value, "");
66 }
67 {
68 // test that cv void is guarded against as required.
69 static_assert(!std::is_swappable_with_v<void, int>, "");
70 static_assert(!std::is_swappable_with_v<int, void>, "");
71 static_assert(!std::is_swappable_with_v<const void, const volatile void>, "");
72 }
73 {
74 // test for presence of is_swappable_with_v
75 static_assert(std::is_swappable_with_v<int&, int&>, "");
76 static_assert(!std::is_swappable_with_v<D&, C&>, "");
77 }
78 }
79