1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_OPTIONAL 11#define _LIBCPP_OPTIONAL 12 13/* 14 optional synopsis 15 16// C++1z 17 18namespace std { 19 // [optional.optional], class template optional 20 template <class T> 21 class optional; 22 23 template<class T> 24 concept is-derived-from-optional = requires(const T& t) { // exposition only 25 []<class U>(const optional<U>&){ }(t); 26 }; 27 28 // [optional.nullopt], no-value state indicator 29 struct nullopt_t{see below }; 30 inline constexpr nullopt_t nullopt(unspecified ); 31 32 // [optional.bad.access], class bad_optional_access 33 class bad_optional_access; 34 35 // [optional.relops], relational operators 36 template <class T, class U> 37 constexpr bool operator==(const optional<T>&, const optional<U>&); 38 template <class T, class U> 39 constexpr bool operator!=(const optional<T>&, const optional<U>&); 40 template <class T, class U> 41 constexpr bool operator<(const optional<T>&, const optional<U>&); 42 template <class T, class U> 43 constexpr bool operator>(const optional<T>&, const optional<U>&); 44 template <class T, class U> 45 constexpr bool operator<=(const optional<T>&, const optional<U>&); 46 template <class T, class U> 47 constexpr bool operator>=(const optional<T>&, const optional<U>&); 48 template<class T, three_way_comparable_with<T> U> 49 constexpr compare_three_way_result_t<T, U> 50 operator<=>(const optional<T>&, const optional<U>&); // since C++20 51 52 // [optional.nullops], comparison with nullopt 53 template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept; 54 template<class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept; // until C++17 55 template<class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; // until C++17 56 template<class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; // until C++17 57 template<class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept; // until C++17 58 template<class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept; // until C++17 59 template<class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; // until C++17 60 template<class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; // until C++17 61 template<class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept; // until C++17 62 template<class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept; // until C++17 63 template<class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; // until C++17 64 template<class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; // until C++17 65 template<class T> 66 constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // since C++20 67 68 // [optional.comp.with.t], comparison with T 69 template<class T, class U> constexpr bool operator==(const optional<T>&, const U&); 70 template<class T, class U> constexpr bool operator==(const T&, const optional<U>&); 71 template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&); 72 template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&); 73 template<class T, class U> constexpr bool operator<(const optional<T>&, const U&); 74 template<class T, class U> constexpr bool operator<(const T&, const optional<U>&); 75 template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&); 76 template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&); 77 template<class T, class U> constexpr bool operator>(const optional<T>&, const U&); 78 template<class T, class U> constexpr bool operator>(const T&, const optional<U>&); 79 template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&); 80 template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&); 81 template<class T, class U> 82 requires (!is-derived-from-optional<U>) && three_way_comparable_with<T, U> 83 constexpr compare_three_way_result_t<T, U> 84 operator<=>(const optional<T>&, const U&); // since C++20 85 86 // [optional.specalg], specialized algorithms 87 template<class T> 88 void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20 89 90 template<class T> 91 constexpr optional<see below > make_optional(T&&); 92 template<class T, class... Args> 93 constexpr optional<T> make_optional(Args&&... args); 94 template<class T, class U, class... Args> 95 constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args); 96 97 // [optional.hash], hash support 98 template<class T> struct hash; 99 template<class T> struct hash<optional<T>>; 100 101 template<class T> 102 class optional { 103 public: 104 using value_type = T; 105 106 // [optional.ctor], constructors 107 constexpr optional() noexcept; 108 constexpr optional(nullopt_t) noexcept; 109 constexpr optional(const optional &); 110 constexpr optional(optional &&) noexcept(see below); 111 template<class... Args> 112 constexpr explicit optional(in_place_t, Args &&...); 113 template<class U, class... Args> 114 constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...); 115 template<class U = T> 116 constexpr explicit(see-below) optional(U &&); 117 template<class U> 118 explicit(see-below) optional(const optional<U> &); // constexpr in C++20 119 template<class U> 120 explicit(see-below) optional(optional<U> &&); // constexpr in C++20 121 122 // [optional.dtor], destructor 123 ~optional(); // constexpr in C++20 124 125 // [optional.assign], assignment 126 optional &operator=(nullopt_t) noexcept; // constexpr in C++20 127 constexpr optional &operator=(const optional &); 128 constexpr optional &operator=(optional &&) noexcept(see below); 129 template<class U = T> optional &operator=(U &&); // constexpr in C++20 130 template<class U> optional &operator=(const optional<U> &); // constexpr in C++20 131 template<class U> optional &operator=(optional<U> &&); // constexpr in C++20 132 template<class... Args> T& emplace(Args &&...); // constexpr in C++20 133 template<class U, class... Args> T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20 134 135 // [optional.swap], swap 136 void swap(optional &) noexcept(see below ); // constexpr in C++20 137 138 // [optional.observe], observers 139 constexpr T const *operator->() const; 140 constexpr T *operator->(); 141 constexpr T const &operator*() const &; 142 constexpr T &operator*() &; 143 constexpr T &&operator*() &&; 144 constexpr const T &&operator*() const &&; 145 constexpr explicit operator bool() const noexcept; 146 constexpr bool has_value() const noexcept; 147 constexpr T const &value() const &; 148 constexpr T &value() &; 149 constexpr T &&value() &&; 150 constexpr const T &&value() const &&; 151 template<class U> constexpr T value_or(U &&) const &; 152 template<class U> constexpr T value_or(U &&) &&; 153 154 // [optional.monadic], monadic operations 155 template<class F> constexpr auto and_then(F&& f) &; // since C++23 156 template<class F> constexpr auto and_then(F&& f) &&; // since C++23 157 template<class F> constexpr auto and_then(F&& f) const&; // since C++23 158 template<class F> constexpr auto and_then(F&& f) const&&; // since C++23 159 template<class F> constexpr auto transform(F&& f) &; // since C++23 160 template<class F> constexpr auto transform(F&& f) &&; // since C++23 161 template<class F> constexpr auto transform(F&& f) const&; // since C++23 162 template<class F> constexpr auto transform(F&& f) const&&; // since C++23 163 template<class F> constexpr optional or_else(F&& f) &&; // since C++23 164 template<class F> constexpr optional or_else(F&& f) const&; // since C++23 165 166 // [optional.mod], modifiers 167 void reset() noexcept; // constexpr in C++20 168 169 private: 170 T *val; // exposition only 171 }; 172 173 template<class T> 174 optional(T) -> optional<T>; 175 176} // namespace std 177 178*/ 179 180#include <__assert> 181#include <__availability> 182#include <__compare/compare_three_way_result.h> 183#include <__compare/three_way_comparable.h> 184#include <__concepts/invocable.h> 185#include <__config> 186#include <__exception/exception.h> 187#include <__functional/hash.h> 188#include <__functional/invoke.h> 189#include <__functional/unary_function.h> 190#include <__fwd/functional.h> 191#include <__memory/addressof.h> 192#include <__memory/construct_at.h> 193#include <__tuple/sfinae_helpers.h> 194#include <__type_traits/add_pointer.h> 195#include <__type_traits/conditional.h> 196#include <__type_traits/conjunction.h> 197#include <__type_traits/decay.h> 198#include <__type_traits/disjunction.h> 199#include <__type_traits/is_array.h> 200#include <__type_traits/is_assignable.h> 201#include <__type_traits/is_constructible.h> 202#include <__type_traits/is_convertible.h> 203#include <__type_traits/is_destructible.h> 204#include <__type_traits/is_nothrow_assignable.h> 205#include <__type_traits/is_nothrow_constructible.h> 206#include <__type_traits/is_object.h> 207#include <__type_traits/is_reference.h> 208#include <__type_traits/is_scalar.h> 209#include <__type_traits/is_swappable.h> 210#include <__type_traits/is_trivially_assignable.h> 211#include <__type_traits/is_trivially_constructible.h> 212#include <__type_traits/is_trivially_destructible.h> 213#include <__type_traits/negation.h> 214#include <__type_traits/remove_const.h> 215#include <__type_traits/remove_cvref.h> 216#include <__type_traits/remove_reference.h> 217#include <__utility/declval.h> 218#include <__utility/forward.h> 219#include <__utility/in_place.h> 220#include <__utility/move.h> 221#include <__utility/swap.h> 222#include <__verbose_abort> 223#include <initializer_list> 224#include <new> 225#include <version> 226 227// standard-mandated includes 228 229// [optional.syn] 230#include <compare> 231 232#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 233# pragma GCC system_header 234#endif 235 236_LIBCPP_PUSH_MACROS 237#include <__undef_macros> 238 239namespace std // purposefully not using versioning namespace 240{ 241 242class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access : public exception { 243public: 244 _LIBCPP_HIDE_FROM_ABI bad_optional_access() _NOEXCEPT = default; 245 _LIBCPP_HIDE_FROM_ABI bad_optional_access(const bad_optional_access&) _NOEXCEPT = default; 246 _LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default; 247 // Get the key function ~bad_optional_access() into the dylib 248 ~bad_optional_access() _NOEXCEPT override; 249 const char* what() const _NOEXCEPT override; 250}; 251 252} // namespace std 253 254#if _LIBCPP_STD_VER >= 17 255 256_LIBCPP_BEGIN_NAMESPACE_STD 257 258_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS void 259__throw_bad_optional_access() { 260# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 261 throw bad_optional_access(); 262# else 263 _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode"); 264# endif 265} 266 267struct nullopt_t { 268 struct __secret_tag { 269 explicit __secret_tag() = default; 270 }; 271 _LIBCPP_HIDE_FROM_ABI constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {} 272}; 273 274inline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}}; 275 276struct __optional_construct_from_invoke_tag {}; 277 278template <class _Tp, bool = is_trivially_destructible<_Tp>::value> 279struct __optional_destruct_base; 280 281template <class _Tp> 282struct __optional_destruct_base<_Tp, false> { 283 typedef _Tp value_type; 284 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior"); 285 union { 286 char __null_state_; 287 value_type __val_; 288 }; 289 bool __engaged_; 290 291 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__optional_destruct_base() { 292 if (__engaged_) 293 __val_.~value_type(); 294 } 295 296 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {} 297 298 template <class... _Args> 299 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args) 300 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {} 301 302# if _LIBCPP_STD_VER >= 23 303 template <class _Fp, class... _Args> 304 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base( 305 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) 306 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {} 307# endif 308 309 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { 310 if (__engaged_) { 311 __val_.~value_type(); 312 __engaged_ = false; 313 } 314 } 315}; 316 317template <class _Tp> 318struct __optional_destruct_base<_Tp, true> { 319 typedef _Tp value_type; 320 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior"); 321 union { 322 char __null_state_; 323 value_type __val_; 324 }; 325 bool __engaged_; 326 327 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {} 328 329 template <class... _Args> 330 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args) 331 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {} 332 333# if _LIBCPP_STD_VER >= 23 334 template <class _Fp, class... _Args> 335 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base( 336 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) 337 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {} 338# endif 339 340 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { 341 if (__engaged_) { 342 __engaged_ = false; 343 } 344 } 345}; 346 347template <class _Tp, bool = is_reference<_Tp>::value> 348struct __optional_storage_base : __optional_destruct_base<_Tp> { 349 using __base = __optional_destruct_base<_Tp>; 350 using value_type = _Tp; 351 using __base::__base; 352 353 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; } 354 355 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; } 356 _LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; } 357 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() && noexcept { return std::move(this->__val_); } 358 _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& __get() const&& noexcept { return std::move(this->__val_); } 359 360 template <class... _Args> 361 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_Args&&... __args) { 362 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage"); 363 std::__construct_at(std::addressof(this->__val_), std::forward<_Args>(__args)...); 364 this->__engaged_ = true; 365 } 366 367 template <class _That> 368 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) { 369 if (__opt.has_value()) 370 __construct(std::forward<_That>(__opt).__get()); 371 } 372 373 template <class _That> 374 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) { 375 if (this->__engaged_ == __opt.has_value()) { 376 if (this->__engaged_) 377 this->__val_ = std::forward<_That>(__opt).__get(); 378 } else { 379 if (this->__engaged_) 380 this->reset(); 381 else 382 __construct(std::forward<_That>(__opt).__get()); 383 } 384 } 385}; 386 387// optional<T&> is currently required to be ill-formed. However, it may 388// be allowed in the future. For this reason, it has already been implemented 389// to ensure we can make the change in an ABI-compatible manner. 390template <class _Tp> 391struct __optional_storage_base<_Tp, true> { 392 using value_type = _Tp; 393 using __raw_type = remove_reference_t<_Tp>; 394 __raw_type* __value_; 395 396 template <class _Up> 397 static _LIBCPP_HIDE_FROM_ABI constexpr bool __can_bind_reference() { 398 using _RawUp = __libcpp_remove_reference_t<_Up>; 399 using _UpPtr = _RawUp*; 400 using _RawTp = __libcpp_remove_reference_t<_Tp>; 401 using _TpPtr = _RawTp*; 402 using _CheckLValueArg = 403 integral_constant<bool, 404 (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value) || 405 is_same<_RawUp, reference_wrapper<_RawTp>>::value || 406 is_same<_RawUp, reference_wrapper<__remove_const_t<_RawTp>>>::value >; 407 return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value) || 408 (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value && 409 is_convertible<_UpPtr, _TpPtr>::value); 410 } 411 412 _LIBCPP_HIDE_FROM_ABI constexpr __optional_storage_base() noexcept : __value_(nullptr) {} 413 414 template <class _UArg> 415 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg) 416 : __value_(std::addressof(__uarg)) { 417 static_assert(__can_bind_reference<_UArg>(), 418 "Attempted to construct a reference element in tuple from a " 419 "possible temporary"); 420 } 421 422 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; } 423 424 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; } 425 426 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const& noexcept { return *__value_; } 427 428 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() const&& noexcept { return std::forward<value_type>(*__value_); } 429 430 template <class _UArg> 431 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_UArg&& __val) { 432 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage"); 433 static_assert(__can_bind_reference<_UArg>(), 434 "Attempted to construct a reference element in tuple from a " 435 "possible temporary"); 436 __value_ = std::addressof(__val); 437 } 438 439 template <class _That> 440 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) { 441 if (__opt.has_value()) 442 __construct(std::forward<_That>(__opt).__get()); 443 } 444 445 template <class _That> 446 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) { 447 if (has_value() == __opt.has_value()) { 448 if (has_value()) 449 *__value_ = std::forward<_That>(__opt).__get(); 450 } else { 451 if (has_value()) 452 reset(); 453 else 454 __construct(std::forward<_That>(__opt).__get()); 455 } 456 } 457}; 458 459template <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value> 460struct __optional_copy_base : __optional_storage_base<_Tp> { 461 using __optional_storage_base<_Tp>::__optional_storage_base; 462}; 463 464template <class _Tp> 465struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> { 466 using __optional_storage_base<_Tp>::__optional_storage_base; 467 468 _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default; 469 470 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) { 471 this->__construct_from(__opt); 472 } 473 474 _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&) = default; 475 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default; 476 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&) = default; 477}; 478 479template <class _Tp, bool = is_trivially_move_constructible<_Tp>::value> 480struct __optional_move_base : __optional_copy_base<_Tp> { 481 using __optional_copy_base<_Tp>::__optional_copy_base; 482}; 483 484template <class _Tp> 485struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> { 486 using value_type = _Tp; 487 using __optional_copy_base<_Tp>::__optional_copy_base; 488 489 _LIBCPP_HIDE_FROM_ABI __optional_move_base() = default; 490 _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default; 491 492 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 493 __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) { 494 this->__construct_from(std::move(__opt)); 495 } 496 497 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default; 498 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&) = default; 499}; 500 501template <class _Tp, 502 bool = is_trivially_destructible<_Tp>::value && is_trivially_copy_constructible<_Tp>::value && 503 is_trivially_copy_assignable<_Tp>::value> 504struct __optional_copy_assign_base : __optional_move_base<_Tp> { 505 using __optional_move_base<_Tp>::__optional_move_base; 506}; 507 508template <class _Tp> 509struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> { 510 using __optional_move_base<_Tp>::__optional_move_base; 511 512 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base() = default; 513 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default; 514 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&) = default; 515 516 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base& 517 operator=(const __optional_copy_assign_base& __opt) { 518 this->__assign_from(__opt); 519 return *this; 520 } 521 522 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default; 523}; 524 525template <class _Tp, 526 bool = is_trivially_destructible<_Tp>::value && is_trivially_move_constructible<_Tp>::value && 527 is_trivially_move_assignable<_Tp>::value> 528struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> { 529 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; 530}; 531 532template <class _Tp> 533struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> { 534 using value_type = _Tp; 535 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; 536 537 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base() = default; 538 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt) = default; 539 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&) = default; 540 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default; 541 542 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base& 543 operator=(__optional_move_assign_base&& __opt) noexcept( 544 is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) { 545 this->__assign_from(std::move(__opt)); 546 return *this; 547 } 548}; 549 550template <class _Tp> 551using __optional_sfinae_ctor_base_t = 552 __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >; 553 554template <class _Tp> 555using __optional_sfinae_assign_base_t = 556 __sfinae_assign_base< (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value), 557 (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value) >; 558 559template <class _Tp> 560class optional; 561 562# if _LIBCPP_STD_VER >= 20 563 564template <class _Tp> 565concept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(const optional<_Up>&) {}(__t); }; 566 567# endif // _LIBCPP_STD_VER >= 20 568 569template <class _Tp> 570struct __is_std_optional : false_type {}; 571template <class _Tp> 572struct __is_std_optional<optional<_Tp>> : true_type {}; 573 574template <class _Tp> 575class _LIBCPP_DECLSPEC_EMPTY_BASES optional 576 : private __optional_move_assign_base<_Tp>, 577 private __optional_sfinae_ctor_base_t<_Tp>, 578 private __optional_sfinae_assign_base_t<_Tp> { 579 using __base = __optional_move_assign_base<_Tp>; 580 581public: 582 using value_type = _Tp; 583 584private: 585 // Disable the reference extension using this static assert. 586 static_assert(!is_same_v<__remove_cvref_t<value_type>, in_place_t>, 587 "instantiation of optional with in_place_t is ill-formed"); 588 static_assert(!is_same_v<__remove_cvref_t<value_type>, nullopt_t>, 589 "instantiation of optional with nullopt_t is ill-formed"); 590 static_assert(!is_reference_v<value_type>, "instantiation of optional with a reference type is ill-formed"); 591 static_assert(is_destructible_v<value_type>, "instantiation of optional with a non-destructible type is ill-formed"); 592 static_assert(!is_array_v<value_type>, "instantiation of optional with an array type is ill-formed"); 593 594 // LWG2756: conditionally explicit conversion from _Up 595 struct _CheckOptionalArgsConstructor { 596 template <class _Up> 597 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() { 598 return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>; 599 } 600 601 template <class _Up> 602 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() { 603 return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>; 604 } 605 }; 606 template <class _Up> 607 using _CheckOptionalArgsCtor = 608 _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value && 609 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value), 610 _CheckOptionalArgsConstructor, 611 __check_tuple_constructor_fail >; 612 template <class _QualUp> 613 struct _CheckOptionalLikeConstructor { 614 template <class _Up, class _Opt = optional<_Up>> 615 using __check_constructible_from_opt = 616 _Or< is_constructible<_Tp, _Opt&>, 617 is_constructible<_Tp, _Opt const&>, 618 is_constructible<_Tp, _Opt&&>, 619 is_constructible<_Tp, _Opt const&&>, 620 is_convertible<_Opt&, _Tp>, 621 is_convertible<_Opt const&, _Tp>, 622 is_convertible<_Opt&&, _Tp>, 623 is_convertible<_Opt const&&, _Tp> >; 624 template <class _Up, class _Opt = optional<_Up>> 625 using __check_assignable_from_opt = 626 _Or< is_assignable<_Tp&, _Opt&>, 627 is_assignable<_Tp&, _Opt const&>, 628 is_assignable<_Tp&, _Opt&&>, 629 is_assignable<_Tp&, _Opt const&&> >; 630 template <class _Up, class _QUp = _QualUp> 631 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() { 632 return is_convertible<_QUp, _Tp>::value && 633 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value); 634 } 635 template <class _Up, class _QUp = _QualUp> 636 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() { 637 return !is_convertible<_QUp, _Tp>::value && 638 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value); 639 } 640 template <class _Up, class _QUp = _QualUp> 641 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() { 642 // Construction and assignability of _QUp to _Tp has already been 643 // checked. 644 return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value; 645 } 646 }; 647 648 template <class _Up, class _QualUp> 649 using _CheckOptionalLikeCtor = 650 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value, 651 _CheckOptionalLikeConstructor<_QualUp>, 652 __check_tuple_constructor_fail >; 653 template <class _Up, class _QualUp> 654 using _CheckOptionalLikeAssign = 655 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value, 656 _CheckOptionalLikeConstructor<_QualUp>, 657 __check_tuple_constructor_fail >; 658 659public: 660 _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {} 661 _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default; 662 _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&) = default; 663 _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {} 664 665 template < 666 class _InPlaceT, 667 class... _Args, 668 class = enable_if_t< _And< _IsSame<_InPlaceT, in_place_t>, is_constructible<value_type, _Args...> >::value > > 669 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args) 670 : __base(in_place, std::forward<_Args>(__args)...) {} 671 672 template <class _Up, 673 class... _Args, 674 class = enable_if_t< is_constructible_v<value_type, initializer_list<_Up>&, _Args...>> > 675 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args) 676 : __base(in_place, __il, std::forward<_Args>(__args)...) {} 677 678 template <class _Up = value_type, 679 enable_if_t< _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0> 680 _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {} 681 682 template <class _Up, enable_if_t< _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0> 683 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {} 684 685 // LWG2756: conditionally explicit conversion from const optional<_Up>& 686 template <class _Up, 687 enable_if_t< _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0> 688 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v) { 689 this->__construct_from(__v); 690 } 691 template <class _Up, 692 enable_if_t< _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0> 693 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v) { 694 this->__construct_from(__v); 695 } 696 697 // LWG2756: conditionally explicit conversion from optional<_Up>&& 698 template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0> 699 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v) { 700 this->__construct_from(std::move(__v)); 701 } 702 template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0> 703 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v) { 704 this->__construct_from(std::move(__v)); 705 } 706 707# if _LIBCPP_STD_VER >= 23 708 template <class _Fp, class... _Args> 709 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) 710 : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {} 711# endif 712 713 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept { 714 reset(); 715 return *this; 716 } 717 718 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default; 719 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default; 720 721 // LWG2756 722 template < 723 class _Up = value_type, 724 class = enable_if_t< _And< _IsNotSame<__remove_cvref_t<_Up>, optional>, 725 _Or< _IsNotSame<__remove_cvref_t<_Up>, value_type>, _Not<is_scalar<value_type>> >, 726 is_constructible<value_type, _Up>, 727 is_assignable<value_type&, _Up> >::value> > 728 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) { 729 if (this->has_value()) 730 this->__get() = std::forward<_Up>(__v); 731 else 732 this->__construct(std::forward<_Up>(__v)); 733 return *this; 734 } 735 736 // LWG2756 737 template <class _Up, 738 enable_if_t< _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0> 739 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) { 740 this->__assign_from(__v); 741 return *this; 742 } 743 744 // LWG2756 745 template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0> 746 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) { 747 this->__assign_from(std::move(__v)); 748 return *this; 749 } 750 751 template <class... _Args, class = enable_if_t< is_constructible_v<value_type, _Args...> > > 752 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) { 753 reset(); 754 this->__construct(std::forward<_Args>(__args)...); 755 return this->__get(); 756 } 757 758 template <class _Up, 759 class... _Args, 760 class = enable_if_t< is_constructible_v<value_type, initializer_list<_Up>&, _Args...> > > 761 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { 762 reset(); 763 this->__construct(__il, std::forward<_Args>(__args)...); 764 return this->__get(); 765 } 766 767 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void 768 swap(optional& __opt) noexcept(is_nothrow_move_constructible_v<value_type> && is_nothrow_swappable_v<value_type>) { 769 if (this->has_value() == __opt.has_value()) { 770 using std::swap; 771 if (this->has_value()) 772 swap(this->__get(), __opt.__get()); 773 } else { 774 if (this->has_value()) { 775 __opt.__construct(std::move(this->__get())); 776 reset(); 777 } else { 778 this->__construct(std::move(__opt.__get())); 779 __opt.reset(); 780 } 781 } 782 } 783 784 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type const> operator->() const { 785 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value"); 786 return std::addressof(this->__get()); 787 } 788 789 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type> operator->() { 790 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value"); 791 return std::addressof(this->__get()); 792 } 793 794 _LIBCPP_HIDE_FROM_ABI constexpr const value_type& operator*() const& noexcept { 795 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value"); 796 return this->__get(); 797 } 798 799 _LIBCPP_HIDE_FROM_ABI constexpr value_type& operator*() & noexcept { 800 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value"); 801 return this->__get(); 802 } 803 804 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& operator*() && noexcept { 805 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value"); 806 return std::move(this->__get()); 807 } 808 809 _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& operator*() const&& noexcept { 810 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value"); 811 return std::move(this->__get()); 812 } 813 814 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); } 815 816 using __base::__get; 817 using __base::has_value; 818 819 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type const& value() const& { 820 if (!this->has_value()) 821 __throw_bad_optional_access(); 822 return this->__get(); 823 } 824 825 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type& value() & { 826 if (!this->has_value()) 827 __throw_bad_optional_access(); 828 return this->__get(); 829 } 830 831 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type&& value() && { 832 if (!this->has_value()) 833 __throw_bad_optional_access(); 834 return std::move(this->__get()); 835 } 836 837 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type const&& value() const&& { 838 if (!this->has_value()) 839 __throw_bad_optional_access(); 840 return std::move(this->__get()); 841 } 842 843 template <class _Up> 844 _LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) const& { 845 static_assert(is_copy_constructible_v<value_type>, "optional<T>::value_or: T must be copy constructible"); 846 static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T"); 847 return this->has_value() ? this->__get() : static_cast<value_type>(std::forward<_Up>(__v)); 848 } 849 850 template <class _Up> 851 _LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) && { 852 static_assert(is_move_constructible_v<value_type>, "optional<T>::value_or: T must be move constructible"); 853 static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T"); 854 return this->has_value() ? std::move(this->__get()) : static_cast<value_type>(std::forward<_Up>(__v)); 855 } 856 857# if _LIBCPP_STD_VER >= 23 858 template <class _Func> 859 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) & { 860 using _Up = invoke_result_t<_Func, value_type&>; 861 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, 862 "Result of f(value()) must be a specialization of std::optional"); 863 if (*this) 864 return std::invoke(std::forward<_Func>(__f), value()); 865 return remove_cvref_t<_Up>(); 866 } 867 868 template <class _Func> 869 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) const& { 870 using _Up = invoke_result_t<_Func, const value_type&>; 871 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, 872 "Result of f(value()) must be a specialization of std::optional"); 873 if (*this) 874 return std::invoke(std::forward<_Func>(__f), value()); 875 return remove_cvref_t<_Up>(); 876 } 877 878 template <class _Func> 879 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) && { 880 using _Up = invoke_result_t<_Func, value_type&&>; 881 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, 882 "Result of f(std::move(value())) must be a specialization of std::optional"); 883 if (*this) 884 return std::invoke(std::forward<_Func>(__f), std::move(value())); 885 return remove_cvref_t<_Up>(); 886 } 887 888 template <class _Func> 889 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& { 890 using _Up = invoke_result_t<_Func, const value_type&&>; 891 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, 892 "Result of f(std::move(value())) must be a specialization of std::optional"); 893 if (*this) 894 return std::invoke(std::forward<_Func>(__f), std::move(value())); 895 return remove_cvref_t<_Up>(); 896 } 897 898 template <class _Func> 899 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) & { 900 using _Up = remove_cv_t<invoke_result_t<_Func, value_type&>>; 901 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array"); 902 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t"); 903 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t"); 904 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type"); 905 if (*this) 906 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value()); 907 return optional<_Up>(); 908 } 909 910 template <class _Func> 911 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) const& { 912 using _Up = remove_cv_t<invoke_result_t<_Func, const value_type&>>; 913 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array"); 914 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t"); 915 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t"); 916 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type"); 917 if (*this) 918 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value()); 919 return optional<_Up>(); 920 } 921 922 template <class _Func> 923 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) && { 924 using _Up = remove_cv_t<invoke_result_t<_Func, value_type&&>>; 925 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array"); 926 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t"); 927 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t"); 928 static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type"); 929 if (*this) 930 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value())); 931 return optional<_Up>(); 932 } 933 934 template <class _Func> 935 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) const&& { 936 using _Up = remove_cvref_t<invoke_result_t<_Func, const value_type&&>>; 937 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array"); 938 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t"); 939 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t"); 940 static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type"); 941 if (*this) 942 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value())); 943 return optional<_Up>(); 944 } 945 946 template <invocable _Func> 947 _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const& 948 requires is_copy_constructible_v<value_type> 949 { 950 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>, 951 "Result of f() should be the same type as this optional"); 952 if (*this) 953 return *this; 954 return std::forward<_Func>(__f)(); 955 } 956 957 template <invocable _Func> 958 _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) && 959 requires is_move_constructible_v<value_type> 960 { 961 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>, 962 "Result of f() should be the same type as this optional"); 963 if (*this) 964 return std::move(*this); 965 return std::forward<_Func>(__f)(); 966 } 967# endif // _LIBCPP_STD_VER >= 23 968 969 using __base::reset; 970}; 971 972# if _LIBCPP_STD_VER >= 17 973template <class _Tp> 974optional(_Tp) -> optional<_Tp>; 975# endif 976 977// Comparisons between optionals 978template <class _Tp, class _Up> 979_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 980 is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>, 981 bool > 982operator==(const optional<_Tp>& __x, const optional<_Up>& __y) { 983 if (static_cast<bool>(__x) != static_cast<bool>(__y)) 984 return false; 985 if (!static_cast<bool>(__x)) 986 return true; 987 return *__x == *__y; 988} 989 990template <class _Tp, class _Up> 991_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 992 is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>, 993 bool > 994operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) { 995 if (static_cast<bool>(__x) != static_cast<bool>(__y)) 996 return true; 997 if (!static_cast<bool>(__x)) 998 return false; 999 return *__x != *__y; 1000} 1001 1002template <class _Tp, class _Up> 1003_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1004 is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>, 1005 bool > 1006operator<(const optional<_Tp>& __x, const optional<_Up>& __y) { 1007 if (!static_cast<bool>(__y)) 1008 return false; 1009 if (!static_cast<bool>(__x)) 1010 return true; 1011 return *__x < *__y; 1012} 1013 1014template <class _Tp, class _Up> 1015_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1016 is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>, 1017 bool > 1018operator>(const optional<_Tp>& __x, const optional<_Up>& __y) { 1019 if (!static_cast<bool>(__x)) 1020 return false; 1021 if (!static_cast<bool>(__y)) 1022 return true; 1023 return *__x > *__y; 1024} 1025 1026template <class _Tp, class _Up> 1027_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1028 is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>, 1029 bool > 1030operator<=(const optional<_Tp>& __x, const optional<_Up>& __y) { 1031 if (!static_cast<bool>(__x)) 1032 return true; 1033 if (!static_cast<bool>(__y)) 1034 return false; 1035 return *__x <= *__y; 1036} 1037 1038template <class _Tp, class _Up> 1039_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1040 is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>, 1041 bool > 1042operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) { 1043 if (!static_cast<bool>(__y)) 1044 return true; 1045 if (!static_cast<bool>(__x)) 1046 return false; 1047 return *__x >= *__y; 1048} 1049 1050# if _LIBCPP_STD_VER >= 20 1051 1052template <class _Tp, three_way_comparable_with<_Tp> _Up> 1053_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up> 1054operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) { 1055 if (__x && __y) 1056 return *__x <=> *__y; 1057 return __x.has_value() <=> __y.has_value(); 1058} 1059 1060# endif // _LIBCPP_STD_VER >= 20 1061 1062// Comparisons with nullopt 1063template <class _Tp> 1064_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept { 1065 return !static_cast<bool>(__x); 1066} 1067 1068# if _LIBCPP_STD_VER <= 17 1069 1070template <class _Tp> 1071_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(nullopt_t, const optional<_Tp>& __x) noexcept { 1072 return !static_cast<bool>(__x); 1073} 1074 1075template <class _Tp> 1076_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, nullopt_t) noexcept { 1077 return static_cast<bool>(__x); 1078} 1079 1080template <class _Tp> 1081_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(nullopt_t, const optional<_Tp>& __x) noexcept { 1082 return static_cast<bool>(__x); 1083} 1084 1085template <class _Tp> 1086_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>&, nullopt_t) noexcept { 1087 return false; 1088} 1089 1090template <class _Tp> 1091_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(nullopt_t, const optional<_Tp>& __x) noexcept { 1092 return static_cast<bool>(__x); 1093} 1094 1095template <class _Tp> 1096_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, nullopt_t) noexcept { 1097 return !static_cast<bool>(__x); 1098} 1099 1100template <class _Tp> 1101_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(nullopt_t, const optional<_Tp>&) noexcept { 1102 return true; 1103} 1104 1105template <class _Tp> 1106_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, nullopt_t) noexcept { 1107 return static_cast<bool>(__x); 1108} 1109 1110template <class _Tp> 1111_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(nullopt_t, const optional<_Tp>&) noexcept { 1112 return false; 1113} 1114 1115template <class _Tp> 1116_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>&, nullopt_t) noexcept { 1117 return true; 1118} 1119 1120template <class _Tp> 1121_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(nullopt_t, const optional<_Tp>& __x) noexcept { 1122 return !static_cast<bool>(__x); 1123} 1124 1125# else // _LIBCPP_STD_VER <= 17 1126 1127template <class _Tp> 1128_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept { 1129 return __x.has_value() <=> false; 1130} 1131 1132# endif // _LIBCPP_STD_VER <= 17 1133 1134// Comparisons with T 1135template <class _Tp, class _Up> 1136_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1137 is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>, 1138 bool > 1139operator==(const optional<_Tp>& __x, const _Up& __v) { 1140 return static_cast<bool>(__x) ? *__x == __v : false; 1141} 1142 1143template <class _Tp, class _Up> 1144_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1145 is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>, 1146 bool > 1147operator==(const _Tp& __v, const optional<_Up>& __x) { 1148 return static_cast<bool>(__x) ? __v == *__x : false; 1149} 1150 1151template <class _Tp, class _Up> 1152_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1153 is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>, 1154 bool > 1155operator!=(const optional<_Tp>& __x, const _Up& __v) { 1156 return static_cast<bool>(__x) ? *__x != __v : true; 1157} 1158 1159template <class _Tp, class _Up> 1160_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1161 is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>, 1162 bool > 1163operator!=(const _Tp& __v, const optional<_Up>& __x) { 1164 return static_cast<bool>(__x) ? __v != *__x : true; 1165} 1166 1167template <class _Tp, class _Up> 1168_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1169 is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>, 1170 bool > 1171operator<(const optional<_Tp>& __x, const _Up& __v) { 1172 return static_cast<bool>(__x) ? *__x < __v : true; 1173} 1174 1175template <class _Tp, class _Up> 1176_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1177 is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>, 1178 bool > 1179operator<(const _Tp& __v, const optional<_Up>& __x) { 1180 return static_cast<bool>(__x) ? __v < *__x : false; 1181} 1182 1183template <class _Tp, class _Up> 1184_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1185 is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>, 1186 bool > 1187operator<=(const optional<_Tp>& __x, const _Up& __v) { 1188 return static_cast<bool>(__x) ? *__x <= __v : true; 1189} 1190 1191template <class _Tp, class _Up> 1192_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1193 is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>, 1194 bool > 1195operator<=(const _Tp& __v, const optional<_Up>& __x) { 1196 return static_cast<bool>(__x) ? __v <= *__x : false; 1197} 1198 1199template <class _Tp, class _Up> 1200_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1201 is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>, 1202 bool > 1203operator>(const optional<_Tp>& __x, const _Up& __v) { 1204 return static_cast<bool>(__x) ? *__x > __v : false; 1205} 1206 1207template <class _Tp, class _Up> 1208_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1209 is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>, 1210 bool > 1211operator>(const _Tp& __v, const optional<_Up>& __x) { 1212 return static_cast<bool>(__x) ? __v > *__x : true; 1213} 1214 1215template <class _Tp, class _Up> 1216_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1217 is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>, 1218 bool > 1219operator>=(const optional<_Tp>& __x, const _Up& __v) { 1220 return static_cast<bool>(__x) ? *__x >= __v : false; 1221} 1222 1223template <class _Tp, class _Up> 1224_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t< 1225 is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>, 1226 bool > 1227operator>=(const _Tp& __v, const optional<_Up>& __x) { 1228 return static_cast<bool>(__x) ? __v >= *__x : true; 1229} 1230 1231# if _LIBCPP_STD_VER >= 20 1232 1233template <class _Tp, class _Up> 1234 requires(!__is_derived_from_optional<_Up>) && three_way_comparable_with<_Tp, _Up> 1235_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up> 1236operator<=>(const optional<_Tp>& __x, const _Up& __v) { 1237 return __x.has_value() ? *__x <=> __v : strong_ordering::less; 1238} 1239 1240# endif // _LIBCPP_STD_VER >= 20 1241 1242template <class _Tp> 1243inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 1244 enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, void > 1245 swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) { 1246 __x.swap(__y); 1247} 1248 1249template <class _Tp> 1250_LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) { 1251 return optional<decay_t<_Tp>>(std::forward<_Tp>(__v)); 1252} 1253 1254template <class _Tp, class... _Args> 1255_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) { 1256 return optional<_Tp>(in_place, std::forward<_Args>(__args)...); 1257} 1258 1259template <class _Tp, class _Up, class... _Args> 1260_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) { 1261 return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...); 1262} 1263 1264template <class _Tp> 1265struct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > { 1266# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1267 _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type; 1268 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type; 1269# endif 1270 1271 _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const { 1272 return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0; 1273 } 1274}; 1275 1276_LIBCPP_END_NAMESPACE_STD 1277 1278#endif // _LIBCPP_STD_VER >= 17 1279 1280_LIBCPP_POP_MACROS 1281 1282#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 1283# include <atomic> 1284# include <climits> 1285# include <concepts> 1286# include <ctime> 1287# include <iterator> 1288# include <limits> 1289# include <memory> 1290# include <ratio> 1291# include <stdexcept> 1292# include <tuple> 1293# include <type_traits> 1294# include <typeinfo> 1295# include <utility> 1296# include <variant> 1297#endif 1298 1299#endif // _LIBCPP_OPTIONAL 1300