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_swappable
14
15 #include <type_traits>
16 #include <vector>
17 #include "test_macros.h"
18
19 namespace MyNS {
20
21 // Make the test types non-copyable so that generic std::swap is not valid.
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
swap(A &,A &)32 void swap(A&, A&) noexcept {}
swap(B &,B &)33 void swap(B&, B&) {}
34
35 struct M {
36 M(M const&) = delete;
37 M& operator=(M const&) = delete;
38 };
39
swap(M &&,M &&)40 void swap(M&&, M&&) noexcept {}
41
42 struct ThrowingMove {
ThrowingMoveMyNS::ThrowingMove43 ThrowingMove(ThrowingMove&&) {}
operator =MyNS::ThrowingMove44 ThrowingMove& operator=(ThrowingMove&&) { return *this; }
45 };
46
47 } // namespace MyNS
48
main(int,char **)49 int main(int, char**)
50 {
51 using namespace MyNS;
52 {
53 // Test that is_swappable applies an lvalue reference to the type.
54 static_assert(std::is_nothrow_swappable<int>::value, "");
55 static_assert(std::is_nothrow_swappable<int&>::value, "");
56 static_assert(!std::is_nothrow_swappable<M>::value, "");
57 static_assert(!std::is_nothrow_swappable<M&&>::value, "");
58 }
59 {
60 // Test that it correctly deduces the noexcept of swap.
61 static_assert(std::is_nothrow_swappable<A>::value, "");
62 static_assert(!std::is_nothrow_swappable<B>::value
63 && std::is_swappable<B>::value, "");
64 static_assert(!std::is_nothrow_swappable<ThrowingMove>::value
65 && std::is_swappable<ThrowingMove>::value, "");
66 }
67 {
68 // Test that it doesn't drop the qualifiers
69 static_assert(!std::is_nothrow_swappable<const A>::value, "");
70 }
71 {
72 // test non-referenceable types
73 static_assert(!std::is_nothrow_swappable<void>::value, "");
74 static_assert(!std::is_nothrow_swappable<int() const>::value, "");
75 static_assert(!std::is_nothrow_swappable<int(int, ...) const &>::value, "");
76 }
77 {
78 // test for presence of is_nothrow_swappable_v
79 static_assert(std::is_nothrow_swappable_v<int>, "");
80 static_assert(!std::is_nothrow_swappable_v<void>, "");
81 }
82
83 return 0;
84 }
85