| 1 | // { dg-do run { target c++17 } } |
| 2 | // { dg-add-options no_pch } |
| 3 | |
| 4 | // Copyright (C) 2013-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 <optional> |
| 22 | |
| 23 | #ifndef __cpp_lib_optional |
| 24 | # error "Feature test macro for optional is missing in <optional>" |
| 25 | #elif __cpp_lib_optional < 201606L |
| 26 | # error "Feature test macro for optional has wrong value in <optional>" |
| 27 | #elif __cplusplus == 202002L && __cpp_lib_optional != 202106L |
| 28 | # error "Feature test macro for optional has wrong value for C++20 in <optional>" |
| 29 | #elif __cplusplus > 202002L && __cpp_lib_optional != 202110L |
| 30 | # error "Feature test macro for optional has wrong value for C++23 in <version>" |
| 31 | #endif |
| 32 | |
| 33 | #include <testsuite_hooks.h> |
| 34 | |
| 35 | #include <tuple> |
| 36 | |
| 37 | using std::bad_optional_access; |
| 38 | static_assert( std::is_default_constructible<bad_optional_access>::value, "" ); |
| 39 | |
| 40 | struct trivially_destructible |
| 41 | { |
| 42 | trivially_destructible() = delete; |
| 43 | trivially_destructible(trivially_destructible const&) = delete; |
| 44 | trivially_destructible& operator=(trivially_destructible const&) = delete; |
| 45 | trivially_destructible(trivially_destructible&&) = delete; |
| 46 | trivially_destructible& operator=(trivially_destructible&&) = delete; |
| 47 | ~trivially_destructible() noexcept = default; |
| 48 | }; |
| 49 | |
| 50 | static_assert( std::is_trivially_destructible<trivially_destructible>(), "" ); |
| 51 | |
| 52 | struct no_default_constructor |
| 53 | { |
| 54 | no_default_constructor() = delete; |
| 55 | }; |
| 56 | |
| 57 | struct no_copy_constructor |
| 58 | { |
| 59 | no_copy_constructor() = default; |
| 60 | no_copy_constructor(no_copy_constructor const&) = delete; |
| 61 | no_copy_constructor& operator=(no_copy_constructor const&) = default; |
| 62 | no_copy_constructor(no_copy_constructor&&) = default; |
| 63 | no_copy_constructor& operator=(no_copy_constructor&&) = default; |
| 64 | }; |
| 65 | |
| 66 | struct no_copy_assignment |
| 67 | { |
| 68 | no_copy_assignment() = default; |
| 69 | no_copy_assignment(no_copy_assignment const&) = default; |
| 70 | no_copy_assignment(no_copy_assignment&&) = default; |
| 71 | no_copy_assignment& operator=(no_copy_assignment&&) = default; |
| 72 | }; |
| 73 | |
| 74 | struct no_move_constructor |
| 75 | { |
| 76 | no_move_constructor() = default; |
| 77 | no_move_constructor(no_move_constructor const&) = default; |
| 78 | no_move_constructor& operator=(no_move_constructor const&) = default; |
| 79 | no_move_constructor(no_move_constructor&&) = delete; |
| 80 | no_move_constructor& operator=(no_move_constructor&&) = default; |
| 81 | }; |
| 82 | |
| 83 | struct no_move_assignment |
| 84 | { |
| 85 | no_move_assignment() = default; |
| 86 | no_move_assignment(no_move_assignment const&) = default; |
| 87 | no_move_assignment& operator=(no_move_assignment const&) = default; |
| 88 | no_move_assignment(no_move_assignment&&) = default; |
| 89 | no_move_assignment& operator=(no_move_assignment&&) = delete; |
| 90 | }; |
| 91 | |
| 92 | struct no_copy : no_copy_constructor, no_copy_assignment { }; |
| 93 | struct no_move : no_move_constructor, no_move_assignment { }; |
| 94 | |
| 95 | // Laxest possible model of a value type for optional |
| 96 | struct only_destructible |
| 97 | { |
| 98 | only_destructible(only_destructible&&) = delete; |
| 99 | }; |
| 100 | |
| 101 | int main() |
| 102 | { |
| 103 | { |
| 104 | static_assert( std::is_trivially_destructible<std::optional<trivially_destructible>>(), "" ); |
| 105 | } |
| 106 | |
| 107 | { |
| 108 | using T = no_default_constructor; |
| 109 | using O = std::optional<T>; |
| 110 | static_assert( std::is_same<O::value_type, T>(), "" ); |
| 111 | static_assert( std::is_default_constructible<O>(), "" ); |
| 112 | { O o; } |
| 113 | static_assert( std::is_copy_constructible<O>(), "" ); |
| 114 | { O o; auto copy = o; } |
| 115 | static_assert( std::is_copy_assignable<O>(), "" ); |
| 116 | { O o, p; p = o; } |
| 117 | static_assert( std::is_move_constructible<O>(), "" ); |
| 118 | { O o; auto moved_to = std::move(o); } |
| 119 | static_assert( std::is_move_assignable<O>(), "" ); |
| 120 | { O o, p; p = std::move(o); } |
| 121 | } |
| 122 | |
| 123 | { |
| 124 | using T = no_copy_constructor; |
| 125 | using O = std::optional<T>; |
| 126 | static_assert( std::is_same<O::value_type, T>(), "" ); |
| 127 | static_assert( std::is_default_constructible<O>(), "" ); |
| 128 | { O o; } |
| 129 | static_assert( !std::is_copy_constructible<O>(), "" ); |
| 130 | static_assert( !std::is_copy_assignable<O>(), "" ); |
| 131 | static_assert( std::is_move_constructible<O>(), "" ); |
| 132 | { O o; auto moved_to = std::move(o); } |
| 133 | static_assert( std::is_move_assignable<O>(), "" ); |
| 134 | { O o, p; p = std::move(o); } |
| 135 | } |
| 136 | |
| 137 | { |
| 138 | using T = no_copy_assignment; |
| 139 | using O = std::optional<T>; |
| 140 | static_assert( std::is_default_constructible<O>(), "" ); |
| 141 | { O o; } |
| 142 | static_assert( std::is_copy_constructible<O>(), "" ); |
| 143 | { O o; auto copy = o; } |
| 144 | static_assert( !std::is_copy_assignable<O>(), "" ); |
| 145 | static_assert( std::is_move_constructible<O>(), "" ); |
| 146 | { O o; auto moved_to = std::move(o); } |
| 147 | static_assert( std::is_move_assignable<O>(), "" ); |
| 148 | { O o, p; p = std::move(o); } |
| 149 | } |
| 150 | |
| 151 | { |
| 152 | using T = no_copy; |
| 153 | using O = std::optional<T>; |
| 154 | static_assert( std::is_same<O::value_type, T>(), "" ); |
| 155 | static_assert( std::is_default_constructible<O>(), "" ); |
| 156 | { O o; } |
| 157 | static_assert( !std::is_copy_constructible<O>(), "" ); |
| 158 | static_assert( !std::is_copy_assignable<O>(), "" ); |
| 159 | static_assert( std::is_move_constructible<O>(), "" ); |
| 160 | { O o; auto moved_to = std::move(o); } |
| 161 | static_assert( std::is_move_assignable<O>(), "" ); |
| 162 | { O o, p; p = std::move(o); } |
| 163 | } |
| 164 | |
| 165 | { |
| 166 | using T = no_move_constructor; |
| 167 | using O = std::optional<T>; |
| 168 | static_assert( std::is_same<O::value_type, T>(), "" ); |
| 169 | static_assert( std::is_default_constructible<O>(), "" ); |
| 170 | { O o; } |
| 171 | static_assert( std::is_copy_constructible<O>(), "" ); |
| 172 | { O o; auto copy = o; } |
| 173 | static_assert( std::is_copy_assignable<O>(), "" ); |
| 174 | /* |
| 175 | * T should be move constructible due to [12.8/11], which is a new rule in C++1y |
| 176 | * not yet implemented by GCC. Because there is already a special exception in C++11 |
| 177 | * for the generation of the special members that GCC implements (at least some of the |
| 178 | * time), this does not affect the std::optional implementation however. So the assertion |
| 179 | * for T should be changed (or removed altogether) when the time comes, but the rest |
| 180 | * should however remain correct and unchanged. |
| 181 | */ |
| 182 | static_assert( !std::is_move_constructible<T>(), "" ); |
| 183 | static_assert( std::is_move_constructible<O>(), "" ); |
| 184 | { O o; auto moved_to = std::move(o); } |
| 185 | static_assert( std::is_move_assignable<O>(), "" ); |
| 186 | { O o, p; p = std::move(o); } |
| 187 | } |
| 188 | |
| 189 | { |
| 190 | using T = no_move_assignment; |
| 191 | using O = std::optional<T>; |
| 192 | static_assert( std::is_same<O::value_type, T>(), "" ); |
| 193 | static_assert( std::is_default_constructible<O>(), "" ); |
| 194 | { O o; } |
| 195 | static_assert( std::is_copy_constructible<O>(), "" ); |
| 196 | { O o; auto copy = o; } |
| 197 | static_assert( std::is_copy_assignable<O>(), "" ); |
| 198 | { O o, p; p = o; } |
| 199 | static_assert( std::is_move_constructible<O>(), "" ); |
| 200 | { O o; auto moved_to = std::move(o); } |
| 201 | /* |
| 202 | * Paragraph 23 of same leads to a similar situation but with respect to move |
| 203 | * assignment. |
| 204 | */ |
| 205 | static_assert( !std::is_move_assignable<T>(), "" ); |
| 206 | static_assert( std::is_move_assignable<O>(), "" ); |
| 207 | { O o, p; p = std::move(o); } |
| 208 | } |
| 209 | |
| 210 | { |
| 211 | using T = no_move; |
| 212 | using O = std::optional<T>; |
| 213 | static_assert( std::is_same<O::value_type, T>(), "" ); |
| 214 | static_assert( std::is_default_constructible<O>(), "" ); |
| 215 | { O o; } |
| 216 | static_assert( std::is_copy_constructible<O>(), "" ); |
| 217 | { O o; auto copy = o; } |
| 218 | static_assert( std::is_copy_assignable<O>(), "" ); |
| 219 | { O o, p; p = o; } |
| 220 | static_assert( std::is_move_constructible<O>(), "" ); |
| 221 | { O o; auto moved_to = std::move(o); } |
| 222 | static_assert( std::is_move_assignable<O>(), "" ); |
| 223 | { O o, p; p = std::move(o); } |
| 224 | } |
| 225 | |
| 226 | { |
| 227 | using T = only_destructible; |
| 228 | using O = std::optional<T>; |
| 229 | static_assert( std::is_same<O::value_type, T>(), "" ); |
| 230 | static_assert( std::is_default_constructible<O>(), "" ); |
| 231 | { O o; } |
| 232 | static_assert( !std::is_copy_constructible<O>(), "" ); |
| 233 | static_assert( !std::is_copy_assignable<O>(), "" ); |
| 234 | static_assert( !std::is_move_constructible<O>(), "" ); |
| 235 | static_assert( !std::is_move_assignable<O>(), "" ); |
| 236 | } |
| 237 | |
| 238 | { |
| 239 | /* |
| 240 | * Should not complain about 'invalid' specializations as long as |
| 241 | * they're not instantiated. |
| 242 | */ |
| 243 | using A = std::optional<int&>; |
| 244 | using B = std::optional<int&&>; |
| 245 | using C1 = std::optional<std::in_place_t>; |
| 246 | using C2 = std::optional<std::in_place_t const>; |
| 247 | using C3 = std::optional<std::in_place_t volatile>; |
| 248 | using C4 = std::optional<std::in_place_t const volatile>; |
| 249 | using D1 = std::optional<std::nullopt_t>; |
| 250 | using D2 = std::optional<std::nullopt_t const>; |
| 251 | using D3 = std::optional<std::nullopt_t volatile>; |
| 252 | using D4 = std::optional<std::nullopt_t const volatile>; |
| 253 | |
| 254 | using X = std::tuple<A, B, C1, C2, C3, C4, D1, D2, D3, D4>; |
| 255 | } |
| 256 | |
| 257 | { |
| 258 | std::optional<const int> o { 42 }; |
| 259 | static_assert( std::is_same<decltype(o)::value_type, const int>(), "" ); |
| 260 | VERIFY( o ); |
| 261 | VERIFY( *o == 42 ); |
| 262 | } |
| 263 | |
| 264 | { |
| 265 | constexpr std::optional<const int> o { 33 }; |
| 266 | static_assert( std::is_same<decltype(o)::value_type, const int>(), "" ); |
| 267 | static_assert( o, "" ); |
| 268 | static_assert( *o == 33, "" ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | using std::void_t; |
| 273 | using std::declval; |
| 274 | using std::true_type; |
| 275 | using std::false_type; |
| 276 | |
| 277 | template <class T, class = void> |
| 278 | struct is_eq_comparable : false_type {}; |
| 279 | template <class T> |
| 280 | struct is_eq_comparable<T, void_t<decltype(declval<T>() == declval<T>())>> |
| 281 | : true_type {}; |
| 282 | |
| 283 | template <class T, class = void> |
| 284 | struct is_neq_comparable : false_type {}; |
| 285 | template <class T> |
| 286 | struct is_neq_comparable<T, void_t<decltype(declval<T>() != declval<T>())>> |
| 287 | : true_type {}; |
| 288 | |
| 289 | template <class T, class = void> |
| 290 | struct is_lt_comparable : false_type {}; |
| 291 | template <class T> |
| 292 | struct is_lt_comparable<T, void_t<decltype(declval<T>() < declval<T>())>> |
| 293 | : true_type {}; |
| 294 | |
| 295 | template <class T, class = void> |
| 296 | struct is_gt_comparable : false_type {}; |
| 297 | template <class T> |
| 298 | struct is_gt_comparable<T, void_t<decltype(declval<T>() > declval<T>())>> |
| 299 | : true_type {}; |
| 300 | |
| 301 | template <class T, class = void> |
| 302 | struct is_le_comparable : false_type {}; |
| 303 | template <class T> |
| 304 | struct is_le_comparable<T, void_t<decltype(declval<T>() <= declval<T>())>> |
| 305 | : true_type {}; |
| 306 | |
| 307 | template <class T, class = void> |
| 308 | struct is_ge_comparable : false_type {}; |
| 309 | template <class T> |
| 310 | struct is_ge_comparable<T, void_t<decltype(declval<T>() >= declval<T>())>> |
| 311 | : true_type {}; |
| 312 | |
| 313 | using std::optional; |
| 314 | |
| 315 | static_assert(is_eq_comparable<optional<int>>::value, ""); |
| 316 | static_assert(is_neq_comparable<optional<int>>::value, ""); |
| 317 | static_assert(is_lt_comparable<optional<int>>::value, ""); |
| 318 | static_assert(is_gt_comparable<optional<int>>::value, ""); |
| 319 | static_assert(is_le_comparable<optional<int>>::value, ""); |
| 320 | static_assert(is_ge_comparable<optional<int>>::value, ""); |
| 321 | |
| 322 | struct JustEq {}; |
| 323 | bool operator==(const JustEq&, const JustEq&); |
| 324 | |
| 325 | static_assert(is_eq_comparable<optional<JustEq>>::value, ""); |
| 326 | #if __cplusplus == 201703L |
| 327 | // In C++20 operator!= can be synthesized from operator== |
| 328 | static_assert(!is_neq_comparable<optional<JustEq>>::value, ""); |
| 329 | #endif |
| 330 | static_assert(!is_lt_comparable<optional<JustEq>>::value, ""); |
| 331 | static_assert(!is_gt_comparable<optional<JustEq>>::value, ""); |
| 332 | static_assert(!is_le_comparable<optional<JustEq>>::value, ""); |
| 333 | static_assert(!is_ge_comparable<optional<JustEq>>::value, ""); |
| 334 | |
| 335 | struct JustLt {}; |
| 336 | bool operator<(const JustLt&, const JustLt&); |
| 337 | |
| 338 | static_assert(!is_eq_comparable<optional<JustLt>>::value, ""); |
| 339 | static_assert(!is_neq_comparable<optional<JustLt>>::value, ""); |
| 340 | static_assert(is_lt_comparable<optional<JustLt>>::value, ""); |
| 341 | static_assert(!is_gt_comparable<optional<JustLt>>::value, ""); |
| 342 | static_assert(!is_le_comparable<optional<JustLt>>::value, ""); |
| 343 | static_assert(!is_ge_comparable<optional<JustLt>>::value, ""); |
| 344 | |
| 345 | static_assert(!std::is_assignable<optional<JustEq>&, |
| 346 | optional<JustLt>>::value, ""); |
| 347 | static_assert(!std::is_assignable<optional<JustEq>&, |
| 348 | JustLt>::value, ""); |
| 349 | static_assert(!std::is_assignable<optional<JustEq>&, |
| 350 | optional<JustLt>&>::value, ""); |
| 351 | static_assert(!std::is_assignable<optional<JustEq>&, |
| 352 | JustLt&>::value, ""); |