| 1 |
// Copyright (C) 2018-2025 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-do compile { target c++20 } } |
| 19 |
|
| 20 |
#include <type_traits> |
| 21 |
#include <testsuite_tr1.h> |
| 22 |
|
| 23 |
using std::is_nothrow_convertible; |
| 24 |
|
| 25 |
void test01() |
| 26 |
{ |
| 27 |
using namespace __gnu_test; |
| 28 |
|
| 29 |
// Positive conversion tests. |
| 30 |
static_assert(test_relationship<is_nothrow_convertible, |
| 31 |
int, int>(true)); |
| 32 |
static_assert(test_relationship<is_nothrow_convertible, |
| 33 |
int, const int>(true)); |
| 34 |
static_assert(test_relationship<is_nothrow_convertible, |
| 35 |
volatile int, const int>(true)); |
| 36 |
static_assert(test_relationship<is_nothrow_convertible, |
| 37 |
int, float>(true)); |
| 38 |
static_assert(test_relationship<is_nothrow_convertible, |
| 39 |
double, float>(true)); |
| 40 |
static_assert(test_relationship<is_nothrow_convertible, |
| 41 |
float, int>(true)); |
| 42 |
static_assert(test_relationship<is_nothrow_convertible, |
| 43 |
int*, const int*>(true)); |
| 44 |
static_assert(test_relationship<is_nothrow_convertible, |
| 45 |
int*, void*>(true)); |
| 46 |
static_assert(test_relationship<is_nothrow_convertible, |
| 47 |
int[4], int*>(true)); |
| 48 |
static_assert(test_relationship<is_nothrow_convertible, |
| 49 |
float&, int>(true)); |
| 50 |
static_assert(test_relationship<is_nothrow_convertible, |
| 51 |
int, const int&>(true)); |
| 52 |
static_assert(test_relationship<is_nothrow_convertible, |
| 53 |
const int&, int>(true)); |
| 54 |
static_assert(test_relationship<is_nothrow_convertible, |
| 55 |
float, const int&>(true)); |
| 56 |
static_assert(test_relationship<is_nothrow_convertible, |
| 57 |
int(int), int(*)(int)>(true)); |
| 58 |
static_assert(test_relationship<is_nothrow_convertible, |
| 59 |
int(&)(int), int(*)(int)>(true)); |
| 60 |
static_assert(test_relationship<is_nothrow_convertible, |
| 61 |
EnumType, int>(true)); |
| 62 |
static_assert(test_relationship<is_nothrow_convertible, |
| 63 |
ClassType, ClassType>(true)); |
| 64 |
static_assert(test_relationship<is_nothrow_convertible, |
| 65 |
DerivedType, ClassType>(true)); |
| 66 |
static_assert(test_relationship<is_nothrow_convertible, |
| 67 |
DerivedType*, ClassType*>(true)); |
| 68 |
static_assert(test_relationship<is_nothrow_convertible, |
| 69 |
DerivedType&, ClassType&>(true)); |
| 70 |
|
| 71 |
static_assert(test_relationship<is_nothrow_convertible, |
| 72 |
const int, const int&>(true)); |
| 73 |
|
| 74 |
static_assert(test_relationship<is_nothrow_convertible, |
| 75 |
void, void>(true)); |
| 76 |
static_assert(test_relationship<is_nothrow_convertible, |
| 77 |
const void, void>(true)); |
| 78 |
static_assert(test_relationship<is_nothrow_convertible, |
| 79 |
void, volatile void>(true)); |
| 80 |
static_assert(test_relationship<is_nothrow_convertible, |
| 81 |
double&, NoexceptExplicitClass>(true)); |
| 82 |
static_assert(test_relationship<is_nothrow_convertible, |
| 83 |
NoexceptCopyConsClass, |
| 84 |
NoexceptCopyConsClass>(true)); |
| 85 |
static_assert(test_relationship<is_nothrow_convertible, |
| 86 |
const 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 |
NoexceptMoveConsClass, |
| 93 |
NoexceptMoveConsClass>(true)); |
| 94 |
|
| 95 |
static_assert(test_relationship<is_nothrow_convertible, |
| 96 |
int(int), int(&)(int)>(true)); |
| 97 |
|
| 98 |
// Negative conversion tests. |
| 99 |
static_assert(test_relationship<is_nothrow_convertible, |
| 100 |
const int*, int*>(false)); |
| 101 |
static_assert(test_relationship<is_nothrow_convertible, |
| 102 |
int*, float*>(false)); |
| 103 |
static_assert(test_relationship<is_nothrow_convertible, |
| 104 |
const int[4], int*>(false)); |
| 105 |
static_assert(test_relationship<is_nothrow_convertible, |
| 106 |
int[4], int[4]>(false)); |
| 107 |
static_assert(test_relationship<is_nothrow_convertible, |
| 108 |
const int&, int&>(false)); |
| 109 |
static_assert(test_relationship<is_nothrow_convertible, |
| 110 |
float&, int&>(false)); |
| 111 |
static_assert(test_relationship<is_nothrow_convertible, |
| 112 |
float, volatile int&>(false)); |
| 113 |
static_assert(test_relationship<is_nothrow_convertible, |
| 114 |
int(int), int(int)>(false)); |
| 115 |
static_assert(test_relationship<is_nothrow_convertible, |
| 116 |
int(int), int(*)(void)>(false)); |
| 117 |
static_assert(test_relationship<is_nothrow_convertible, |
| 118 |
int(*)(int), int(&)(int)>(false)); |
| 119 |
static_assert(test_relationship<is_nothrow_convertible, |
| 120 |
int, EnumType>(false)); |
| 121 |
static_assert(test_relationship<is_nothrow_convertible, |
| 122 |
int, ClassType>(false)); |
| 123 |
static_assert(test_relationship<is_nothrow_convertible, |
| 124 |
ClassType, DerivedType>(false)); |
| 125 |
static_assert(test_relationship<is_nothrow_convertible, |
| 126 |
ClassType*, DerivedType*>(false)); |
| 127 |
static_assert(test_relationship<is_nothrow_convertible, |
| 128 |
ClassType&, DerivedType&>(false)); |
| 129 |
|
| 130 |
static_assert(test_relationship<is_nothrow_convertible, |
| 131 |
void, int>(false)); |
| 132 |
static_assert(test_relationship<is_nothrow_convertible, |
| 133 |
void, float>(false)); |
| 134 |
static_assert(test_relationship<is_nothrow_convertible, |
| 135 |
void, int(*)(int)>(false)); |
| 136 |
|
| 137 |
// C++0x |
| 138 |
static_assert(test_relationship<is_nothrow_convertible, |
| 139 |
int, void>(false)); |
| 140 |
static_assert(test_relationship<is_nothrow_convertible, |
| 141 |
int[4], void>(false)); |
| 142 |
|
| 143 |
static_assert(test_relationship<is_nothrow_convertible, |
| 144 |
int, int&>(false)); |
| 145 |
static_assert(test_relationship<is_nothrow_convertible, |
| 146 |
float, volatile float&>(false)); |
| 147 |
static_assert(test_relationship<is_nothrow_convertible, |
| 148 |
const volatile int, |
| 149 |
const volatile int&>(false)); |
| 150 |
static_assert(test_relationship<is_nothrow_convertible, |
| 151 |
volatile int, volatile int&>(false)); |
| 152 |
static_assert(test_relationship<is_nothrow_convertible, |
| 153 |
double&, ExplicitClass>(false)); |
| 154 |
static_assert(test_relationship<is_nothrow_convertible, |
| 155 |
int&, ExplicitClass>(false)); |
| 156 |
static_assert(test_relationship<is_nothrow_convertible, |
| 157 |
void*, ExplicitClass>(false)); |
| 158 |
|
| 159 |
static_assert(test_relationship<is_nothrow_convertible, |
| 160 |
ExceptCopyConsClass, |
| 161 |
ExceptCopyConsClass>(false)); |
| 162 |
static_assert(test_relationship<is_nothrow_convertible, |
| 163 |
const 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 |
ExceptMoveConsClass, |
| 170 |
ExceptMoveConsClass>(false)); |
| 171 |
static_assert(test_relationship<is_nothrow_convertible, |
| 172 |
ExceptMoveConsClass&, |
| 173 |
ExceptMoveConsClass>(false)); |
| 174 |
static_assert(test_relationship<is_nothrow_convertible, |
| 175 |
NoexceptMoveConsClass&, |
| 176 |
NoexceptMoveConsClass>(false)); |
| 177 |
} |
| 178 |
|
| 179 |
void test02() |
| 180 |
{ |
| 181 |
struct X { }; |
| 182 |
|
| 183 |
struct Y |
| 184 |
{ |
| 185 |
explicit Y(X) noexcept; // not viable for implicit conversions |
| 186 |
Y(...); |
| 187 |
}; |
| 188 |
|
| 189 |
static_assert(!is_nothrow_convertible<X, Y>::value, ""); |
| 190 |
} |