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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // type_traits
12 
13 // is_nothrow_swappable_with
14 
15 #include <type_traits>
16 #include <vector>
17 #include "test_macros.h"
18 
19 namespace MyNS {
20 
21 struct A {
22   A(A const&) = delete;
23   A& operator=(A const&) = delete;
24 };
25 
26 struct B {
27   B(B const&) = delete;
28   B& operator=(B const&) = delete;
29 };
30 
31 struct C {};
32 struct D {};
33 
swap(A &,A &)34 void swap(A&, A&) {}
35 
swap(A &,B &)36 void swap(A&, B&) noexcept {}
swap(B &,A &)37 void swap(B&, A&) noexcept {}
38 
swap(A &,C &)39 void swap(A&, C&) noexcept {}
swap(C &,A &)40 void swap(C&, A&) {}
41 
42 struct M {};
43 
swap(M &&,M &&)44 void swap(M&&, M&&) noexcept {}
45 
46 } // namespace MyNS
47 
main(int,char **)48 int main(int, char**)
49 {
50     using namespace MyNS;
51     {
52         // Test that is_swappable_with doesn't apply an lvalue reference
53         // to the type. Instead it is up to the user.
54         static_assert(!std::is_nothrow_swappable_with<int, int>::value, "");
55         static_assert(std::is_nothrow_swappable_with<int&, int&>::value, "");
56         static_assert(std::is_nothrow_swappable_with<M, M>::value, "");
57         static_assert(std::is_swappable_with<A&, A&>::value &&
58                       !std::is_nothrow_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_nothrow_swappable_with<A&, B&>::value, "");
64         static_assert(!std::is_nothrow_swappable_with<A&, C&>::value &&
65                       std::is_swappable_with<A&, C&>::value, "");
66         static_assert(!std::is_nothrow_swappable_with<D&, C&>::value, "");
67     }
68     {
69         // test we guard against cv void inputs as required.
70         static_assert(!std::is_nothrow_swappable_with_v<void, int>, "");
71         static_assert(!std::is_nothrow_swappable_with_v<int, void>, "");
72         static_assert(!std::is_nothrow_swappable_with_v<const void, const volatile void>, "");
73 
74     }
75     {
76         // test for presence of is_nothrow_swappable_with_v
77         static_assert(std::is_nothrow_swappable_with_v<int&, int&>, "");
78         static_assert(!std::is_nothrow_swappable_with_v<int&&, int&&>, "");
79     }
80 
81   return 0;
82 }
83