Debian Codegraph @

libstdc++-v3/testsuite/20_util/is_nothrow_swappable_with/value.cc

raw
1 // { dg-do compile { target c++17 } }
2 // { dg-add-options no_pch }
3
4 // Copyright (C) 2016-2025 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <type_traits>
22
23 #ifndef __cpp_lib_is_swappable
24 # error "Feature-test macro for is_nothrow_swappable_with missing"
25 #elif __cpp_lib_is_swappable != 201603
26 # error "Feature-test macro for is_nothrow_swappable_with has wrong value"
27 #endif
28
29 #include <testsuite_tr1.h>
30
31 namespace funny {
32 struct T0 {};
33
34 void swap(T0, T0) noexcept;
35
36 struct T1
37 {
38 friend void swap(T1, T1) noexcept;
39 };
40
41 struct T2 {};
42 struct T3 {};
43
44 void swap(T2, T3) noexcept;
45 void swap(T3, T2) noexcept;
46
47 struct T4 { operator T0() const noexcept; };
48
49 struct F0 {};
50
51 void swap(F0, F0) = delete;
52
53 struct F1 {};
54
55 void swap(F1, F1);
56
57 struct F2 {};
58
59 void swap(F0, F2) noexcept;
60 void swap(F2, F0);
61
62 struct F3
63 {
64 friend void swap(F3, F3) = delete;
65 };
66
67 struct F4
68 {
69 friend void swap(F4, F4);
70 };
71
72 struct F5 { operator T0() const; };
73
74 struct BoolLike {};
75
76 void swap(BoolLike, bool&) noexcept;
77 void swap(bool&, BoolLike) noexcept;
78
79 struct BoolLikeErr {};
80
81 void swap(BoolLikeErr, bool&);
82 void swap(bool&, BoolLikeErr) noexcept;
83 }
84
85 void test01()
86 {
87 using std::is_nothrow_swappable_with;
88 using namespace __gnu_test;
89 // Positive tests.
90 static_assert(test_property<is_nothrow_swappable_with, int&, int&>(true),
91 "");
92 static_assert(test_property<is_nothrow_swappable_with, funny::T0,
93 funny::T0>(true), "");
94 static_assert(test_property<is_nothrow_swappable_with, funny::T0,
95 const funny::T0>(true), "");
96 static_assert(test_property<is_nothrow_swappable_with, funny::T1,
97 funny::T1>(true), "");
98 static_assert(test_property<is_nothrow_swappable_with, funny::T1,
99 const funny::T1>(true), "");
100 static_assert(test_property<is_nothrow_swappable_with, funny::T2,
101 funny::T3>(true), "");
102 static_assert(test_property<is_nothrow_swappable_with, funny::T3,
103 funny::T2>(true), "");
104 static_assert(test_property<is_nothrow_swappable_with, funny::T0,
105 funny::T4>(true), "");
106 static_assert(test_property<is_nothrow_swappable_with, funny::T4,
107 funny::T0>(true), "");
108 static_assert(test_property<is_nothrow_swappable_with, funny::BoolLike,
109 bool&>(true), "");
110 static_assert(test_property<is_nothrow_swappable_with, const funny::BoolLike,
111 bool&>(true), "");
112
113 // Negative tests.
114 static_assert(test_property<is_nothrow_swappable_with, const int&,
115 const int&>(false), "");
116 static_assert(test_property<is_nothrow_swappable_with, int&,
117 unsigned&>(false), "");
118 static_assert(test_property<is_nothrow_swappable_with, funny::F0,
119 funny::F0>(false), "");
120 static_assert(test_property<is_nothrow_swappable_with, funny::F0,
121 const funny::F0>(false), "");
122 static_assert(test_property<is_nothrow_swappable_with, funny::F1,
123 funny::F1>(false), "");
124 static_assert(test_property<is_nothrow_swappable_with, funny::F1,
125 const funny::F1>(false), "");
126 static_assert(test_property<is_nothrow_swappable_with, funny::F0,
127 funny::F2>(false), "");
128 static_assert(test_property<is_nothrow_swappable_with, funny::F2,
129 funny::F0>(false), "");
130 static_assert(test_property<is_nothrow_swappable_with, funny::F3,
131 funny::F3>(false), "");
132 static_assert(test_property<is_nothrow_swappable_with, funny::F3,
133 const funny::F3>(false), "");
134 static_assert(test_property<is_nothrow_swappable_with, funny::F4,
135 funny::F4>(false), "");
136 static_assert(test_property<is_nothrow_swappable_with, funny::F4,
137 const funny::F4>(false), "");
138 static_assert(test_property<is_nothrow_swappable_with, funny::T0,
139 funny::F5>(false), "");
140 static_assert(test_property<is_nothrow_swappable_with, funny::F5,
141 funny::T0>(false), "");
142 static_assert(test_property<is_nothrow_swappable_with, funny::BoolLikeErr,
143 bool&>(false), "");
144 static_assert(test_property<is_nothrow_swappable_with,
145 const funny::BoolLikeErr, bool&>(false), "");
146 }