blob: 54c0a04039880df9f8117036fdca8493daf13ef2 [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_nothrow_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&) noexcept {}
38void swap(B&, A&) noexcept {}
39
40void swap(A&, C&) noexcept {}
41void swap(C&, A&) {}
42
43struct M {};
44
45void swap(M&&, M&&) noexcept {}
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_nothrow_swappable_with<int, int>::value, "");
56 static_assert(std::is_nothrow_swappable_with<int&, int&>::value, "");
57 static_assert(std::is_nothrow_swappable_with<M, M>::value, "");
58 static_assert(std::is_swappable_with<A&, A&>::value &&
59 !std::is_nothrow_swappable_with<A&, A&>::value, "");
60 }
61 {
62 // test that hetrogenius swap is allowed only if both 'swap(A, B)' and
63 // 'swap(B, A)' are valid.
64 static_assert(std::is_nothrow_swappable_with<A&, B&>::value, "");
65 static_assert(!std::is_nothrow_swappable_with<A&, C&>::value &&
66 std::is_swappable_with<A&, C&>::value, "");
67 static_assert(!std::is_nothrow_swappable_with<D&, C&>::value, "");
68 }
69 {
70 // test we guard against cv void inputs as required.
71 static_assert(!std::is_nothrow_swappable_with_v<void, int>);
72 static_assert(!std::is_nothrow_swappable_with_v<int, void>);
73 static_assert(!std::is_nothrow_swappable_with_v<const void, const volatile void>);
74
75 }
76 {
77 // test for presense of is_nothrow_swappable_with_v
78 static_assert(std::is_nothrow_swappable_with_v<int&, int&>);
79 static_assert(!std::is_nothrow_swappable_with_v<int&&, int&&>);
80 }
81}