blob: 9d470c156dc2c29c35f14dbc2c5a1dc14e1fa8ce [file] [log] [blame]
Eric Fiselier8f1e73d2016-04-21 23:38:59 +00001//===----------------------------------------------------------------------===//
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
20namespace MyNS {
21
22struct A {
23 A(A const&) = delete;
24 A& operator=(A const&) = delete;
25};
26
27struct B {
28 B(B const&) = delete;
29 B& operator=(B const&) = delete;
30};
31
32struct C {};
33struct D {};
34
35void swap(A&, A&) {}
36
37void swap(A&, B&) {}
38void swap(B&, A&) {}
39
40void swap(A&, C&) {} // missing swap(C, A)
41void swap(D&, C&) {}
42
43struct M {};
44
45void swap(M&&, M&&) {}
46
47} // namespace MyNS
48
49int 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}