1 // Copyright (C) 2018-2020 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do compile { target c++2a } }
20
21 #include <type_traits>
22 #include <testsuite_tr1.h>
23
24 #ifndef IS_NT_CONVERTIBLE_DEFINED
25 using std::is_nothrow_convertible;
26 #endif
27
test01()28 void test01()
29 {
30 using namespace __gnu_test;
31
32 // Positive conversion tests.
33 static_assert(test_relationship<is_nothrow_convertible,
34 int, int>(true));
35 static_assert(test_relationship<is_nothrow_convertible,
36 int, const int>(true));
37 static_assert(test_relationship<is_nothrow_convertible,
38 volatile int, const int>(true));
39 static_assert(test_relationship<is_nothrow_convertible,
40 int, float>(true));
41 static_assert(test_relationship<is_nothrow_convertible,
42 double, float>(true));
43 static_assert(test_relationship<is_nothrow_convertible,
44 float, int>(true));
45 static_assert(test_relationship<is_nothrow_convertible,
46 int*, const int*>(true));
47 static_assert(test_relationship<is_nothrow_convertible,
48 int*, void*>(true));
49 static_assert(test_relationship<is_nothrow_convertible,
50 int[4], int*>(true));
51 static_assert(test_relationship<is_nothrow_convertible,
52 float&, int>(true));
53 static_assert(test_relationship<is_nothrow_convertible,
54 int, const int&>(true));
55 static_assert(test_relationship<is_nothrow_convertible,
56 const int&, int>(true));
57 static_assert(test_relationship<is_nothrow_convertible,
58 float, const int&>(true));
59 static_assert(test_relationship<is_nothrow_convertible,
60 int(int), int(*)(int)>(true));
61 static_assert(test_relationship<is_nothrow_convertible,
62 int(&)(int), int(*)(int)>(true));
63 static_assert(test_relationship<is_nothrow_convertible,
64 EnumType, int>(true));
65 static_assert(test_relationship<is_nothrow_convertible,
66 ClassType, ClassType>(true));
67 static_assert(test_relationship<is_nothrow_convertible,
68 DerivedType, ClassType>(true));
69 static_assert(test_relationship<is_nothrow_convertible,
70 DerivedType*, ClassType*>(true));
71 static_assert(test_relationship<is_nothrow_convertible,
72 DerivedType&, ClassType&>(true));
73
74 static_assert(test_relationship<is_nothrow_convertible,
75 const int, const int&>(true));
76
77 static_assert(test_relationship<is_nothrow_convertible,
78 void, void>(true));
79 static_assert(test_relationship<is_nothrow_convertible,
80 const void, void>(true));
81 static_assert(test_relationship<is_nothrow_convertible,
82 void, volatile void>(true));
83 static_assert(test_relationship<is_nothrow_convertible,
84 double&, NoexceptExplicitClass>(true));
85 static_assert(test_relationship<is_nothrow_convertible,
86 NoexceptCopyConsClass,
87 NoexceptCopyConsClass>(true));
88 static_assert(test_relationship<is_nothrow_convertible,
89 const NoexceptCopyConsClass,
90 NoexceptCopyConsClass>(true));
91 static_assert(test_relationship<is_nothrow_convertible,
92 const NoexceptCopyConsClass&,
93 NoexceptCopyConsClass>(true));
94 static_assert(test_relationship<is_nothrow_convertible,
95 NoexceptMoveConsClass,
96 NoexceptMoveConsClass>(true));
97
98 static_assert(test_relationship<is_nothrow_convertible,
99 int(int), int(&)(int)>(true));
100
101 // Negative conversion tests.
102 static_assert(test_relationship<is_nothrow_convertible,
103 const int*, int*>(false));
104 static_assert(test_relationship<is_nothrow_convertible,
105 int*, float*>(false));
106 static_assert(test_relationship<is_nothrow_convertible,
107 const int[4], int*>(false));
108 static_assert(test_relationship<is_nothrow_convertible,
109 int[4], int[4]>(false));
110 static_assert(test_relationship<is_nothrow_convertible,
111 const int&, int&>(false));
112 static_assert(test_relationship<is_nothrow_convertible,
113 float&, int&>(false));
114 static_assert(test_relationship<is_nothrow_convertible,
115 float, volatile int&>(false));
116 static_assert(test_relationship<is_nothrow_convertible,
117 int(int), int(int)>(false));
118 static_assert(test_relationship<is_nothrow_convertible,
119 int(int), int(*)(void)>(false));
120 static_assert(test_relationship<is_nothrow_convertible,
121 int(*)(int), int(&)(int)>(false));
122 static_assert(test_relationship<is_nothrow_convertible,
123 int, EnumType>(false));
124 static_assert(test_relationship<is_nothrow_convertible,
125 int, ClassType>(false));
126 static_assert(test_relationship<is_nothrow_convertible,
127 ClassType, DerivedType>(false));
128 static_assert(test_relationship<is_nothrow_convertible,
129 ClassType*, DerivedType*>(false));
130 static_assert(test_relationship<is_nothrow_convertible,
131 ClassType&, DerivedType&>(false));
132
133 static_assert(test_relationship<is_nothrow_convertible,
134 void, int>(false));
135 static_assert(test_relationship<is_nothrow_convertible,
136 void, float>(false));
137 static_assert(test_relationship<is_nothrow_convertible,
138 void, int(*)(int)>(false));
139
140 // C++0x
141 static_assert(test_relationship<is_nothrow_convertible,
142 int, void>(false));
143 static_assert(test_relationship<is_nothrow_convertible,
144 int[4], void>(false));
145
146 static_assert(test_relationship<is_nothrow_convertible,
147 int, int&>(false));
148 static_assert(test_relationship<is_nothrow_convertible,
149 float, volatile float&>(false));
150 static_assert(test_relationship<is_nothrow_convertible,
151 const volatile int,
152 const volatile int&>(false));
153 static_assert(test_relationship<is_nothrow_convertible,
154 volatile int, volatile int&>(false));
155 static_assert(test_relationship<is_nothrow_convertible,
156 double&, ExplicitClass>(false));
157 static_assert(test_relationship<is_nothrow_convertible,
158 int&, ExplicitClass>(false));
159 static_assert(test_relationship<is_nothrow_convertible,
160 void*, ExplicitClass>(false));
161
162 static_assert(test_relationship<is_nothrow_convertible,
163 ExceptCopyConsClass,
164 ExceptCopyConsClass>(false));
165 static_assert(test_relationship<is_nothrow_convertible,
166 const ExceptCopyConsClass,
167 ExceptCopyConsClass>(false));
168 static_assert(test_relationship<is_nothrow_convertible,
169 const ExceptCopyConsClass&,
170 ExceptCopyConsClass>(false));
171 static_assert(test_relationship<is_nothrow_convertible,
172 ExceptMoveConsClass,
173 ExceptMoveConsClass>(false));
174 static_assert(test_relationship<is_nothrow_convertible,
175 ExceptMoveConsClass&,
176 ExceptMoveConsClass>(false));
177 static_assert(test_relationship<is_nothrow_convertible,
178 NoexceptMoveConsClass&,
179 NoexceptMoveConsClass>(false));
180 }
181
test02()182 void test02()
183 {
184 struct X { };
185
186 struct Y
187 {
188 explicit Y(X) noexcept; // not viable for implicit conversions
189 Y(...);
190 };
191
192 static_assert(!is_nothrow_convertible<X, Y>::value, "");
193 }
194