1559 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP_OPTIONAL11#define _LIBCPP_OPTIONAL12 13/*14 optional synopsis15 16// C++1z17 18namespace std {19 // [optional.optional], class template optional20 template <class T>21 class optional;22 23 template<class T>24 constexpr bool ranges::enable_view<optional<T>> = true;25 template<class T>26 constexpr auto format_kind<optional<T>> = range_format::disabled;27 28 template<class T>29 concept is-derived-from-optional = requires(const T& t) { // exposition only30 []<class U>(const optional<U>&){ }(t);31 };32 33 // [optional.nullopt], no-value state indicator34 struct nullopt_t{see below };35 inline constexpr nullopt_t nullopt(unspecified );36 37 // [optional.bad.access], class bad_optional_access38 class bad_optional_access;39 40 // [optional.relops], relational operators41 template <class T, class U>42 constexpr bool operator==(const optional<T>&, const optional<U>&);43 template <class T, class U>44 constexpr bool operator!=(const optional<T>&, const optional<U>&);45 template <class T, class U>46 constexpr bool operator<(const optional<T>&, const optional<U>&);47 template <class T, class U>48 constexpr bool operator>(const optional<T>&, const optional<U>&);49 template <class T, class U>50 constexpr bool operator<=(const optional<T>&, const optional<U>&);51 template <class T, class U>52 constexpr bool operator>=(const optional<T>&, const optional<U>&);53 template<class T, three_way_comparable_with<T> U>54 constexpr compare_three_way_result_t<T, U>55 operator<=>(const optional<T>&, const optional<U>&); // since C++2056 57 // [optional.nullops], comparison with nullopt58 template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;59 template<class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept; // until C++1760 template<class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; // until C++1761 template<class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; // until C++1762 template<class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept; // until C++1763 template<class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept; // until C++1764 template<class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; // until C++1765 template<class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; // until C++1766 template<class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept; // until C++1767 template<class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept; // until C++1768 template<class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; // until C++1769 template<class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; // until C++1770 template<class T>71 constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // since C++2072 73 // [optional.comp.with.t], comparison with T74 template<class T, class U> constexpr bool operator==(const optional<T>&, const U&);75 template<class T, class U> constexpr bool operator==(const T&, const optional<U>&);76 template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&);77 template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&);78 template<class T, class U> constexpr bool operator<(const optional<T>&, const U&);79 template<class T, class U> constexpr bool operator<(const T&, const optional<U>&);80 template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&);81 template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&);82 template<class T, class U> constexpr bool operator>(const optional<T>&, const U&);83 template<class T, class U> constexpr bool operator>(const T&, const optional<U>&);84 template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&);85 template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&);86 template<class T, class U>87 requires (!is-derived-from-optional<U>) && three_way_comparable_with<T, U>88 constexpr compare_three_way_result_t<T, U>89 operator<=>(const optional<T>&, const U&); // since C++2090 91 // [optional.specalg], specialized algorithms92 template<class T>93 void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++2094 95 template<class T>96 constexpr optional<see below > make_optional(T&&);97 template<class T, class... Args>98 constexpr optional<T> make_optional(Args&&... args);99 template<class T, class U, class... Args>100 constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);101 102 // [optional.hash], hash support103 template<class T> struct hash;104 template<class T> struct hash<optional<T>>;105 106 template<class T>107 class optional {108 public:109 using value_type = T;110 using iterator = implementation-defined; // see [optional.iterators]111 using const_iterator = implementation-defined; // see [optional.iterators]112 113 // [optional.ctor], constructors114 constexpr optional() noexcept;115 constexpr optional(nullopt_t) noexcept;116 constexpr optional(const optional &);117 constexpr optional(optional &&) noexcept(see below);118 template<class... Args>119 constexpr explicit optional(in_place_t, Args &&...);120 template<class U, class... Args>121 constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...);122 template<class U = remove_cv_t<T>>123 constexpr explicit(see-below) optional(U &&);124 template<class U>125 explicit(see-below) optional(const optional<U> &); // constexpr in C++20126 template<class U>127 explicit(see-below) optional(optional<U> &&); // constexpr in C++20128 129 // [optional.dtor], destructor130 ~optional(); // constexpr in C++20131 132 // [optional.assign], assignment133 optional &operator=(nullopt_t) noexcept; // constexpr in C++20134 constexpr optional &operator=(const optional &);135 constexpr optional &operator=(optional &&) noexcept(see below);136 template<class U = remove_cv_t<T>> optional &operator=(U &&); // constexpr in C++20137 template<class U> optional &operator=(const optional<U> &); // constexpr in C++20138 template<class U> optional &operator=(optional<U> &&); // constexpr in C++20139 template<class... Args> T& emplace(Args &&...); // constexpr in C++20140 template<class U, class... Args> T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20141 142 // [optional.swap], swap143 void swap(optional &) noexcept(see below ); // constexpr in C++20144 145 // [optional.iterators], iterator support146 constexpr iterator begin() noexcept;147 constexpr const_iterator begin() const noexcept;148 constexpr iterator end() noexcept;149 constexpr const_iterator end() const noexcept;150 151 // [optional.observe], observers152 constexpr T const *operator->() const noexcept;153 constexpr T *operator->() noexcept;154 constexpr T const &operator*() const & noexcept;155 constexpr T &operator*() & noexcept;156 constexpr T &&operator*() && noexcept;157 constexpr const T &&operator*() const && noexcept;158 constexpr explicit operator bool() const noexcept;159 constexpr bool has_value() const noexcept;160 constexpr T const &value() const &;161 constexpr T &value() &;162 constexpr T &&value() &&;163 constexpr const T &&value() const &&;164 template<class U = remove_cv_t<T>> constexpr T value_or(U &&) const &;165 template<class U = remove_cv_t<T>> constexpr T value_or(U &&) &&;166 167 // [optional.monadic], monadic operations168 template<class F> constexpr auto and_then(F&& f) &; // since C++23169 template<class F> constexpr auto and_then(F&& f) &&; // since C++23170 template<class F> constexpr auto and_then(F&& f) const&; // since C++23171 template<class F> constexpr auto and_then(F&& f) const&&; // since C++23172 template<class F> constexpr auto transform(F&& f) &; // since C++23173 template<class F> constexpr auto transform(F&& f) &&; // since C++23174 template<class F> constexpr auto transform(F&& f) const&; // since C++23175 template<class F> constexpr auto transform(F&& f) const&&; // since C++23176 template<class F> constexpr optional or_else(F&& f) &&; // since C++23177 template<class F> constexpr optional or_else(F&& f) const&; // since C++23178 179 // [optional.mod], modifiers180 void reset() noexcept; // constexpr in C++20181 182 private:183 T *val; // exposition only184 };185 186 template<class T>187 optional(T) -> optional<T>;188 189 template<class T>190 class optional<T&> { // since C++26191 public:192 using value_type = T;193 using iterator = implementation-defined; // see [optional.ref.iterators]194 195 public:196 // [optional.ref.ctor], constructors197 constexpr optional() noexcept = default;198 constexpr optional(nullopt_t) noexcept : optional() {}199 constexpr optional(const optional& rhs) noexcept = default;200 201 template<class Arg>202 constexpr explicit optional(in_place_t, Arg&& arg);203 template<class U>204 constexpr explicit(see below) optional(U&& u) noexcept(see below);205 template<class U>206 constexpr explicit(see below) optional(optional<U>& rhs) noexcept(see below);207 template<class U>208 constexpr explicit(see below) optional(const optional<U>& rhs) noexcept(see below);209 template<class U>210 constexpr explicit(see below) optional(optional<U>&& rhs) noexcept(see below);211 template<class U>212 constexpr explicit(see below) optional(const optional<U>&& rhs) noexcept(see below);213 214 constexpr ~optional() = default;215 216 // [optional.ref.assign], assignment217 constexpr optional& operator=(nullopt_t) noexcept;218 constexpr optional& operator=(const optional& rhs) noexcept = default;219 220 template<class U> constexpr T& emplace(U&& u) noexcept(see below);221 222 // [optional.ref.swap], swap223 constexpr void swap(optional& rhs) noexcept;224 225 // [optional.ref.iterators], iterator support226 constexpr iterator begin() const noexcept;227 constexpr iterator end() const noexcept;228 229 // [optional.ref.observe], observers230 constexpr T* operator->() const noexcept;231 constexpr T& operator*() const noexcept;232 constexpr explicit operator bool() const noexcept;233 constexpr bool has_value() const noexcept;234 constexpr T& value() const; // freestanding-deleted235 template<class U = remove_cv_t<T>>236 constexpr remove_cv_t<T> value_or(U&& u) const;237 238 // [optional.ref.monadic], monadic operations239 template<class F> constexpr auto and_then(F&& f) const;240 template<class F> constexpr optional<invoke_result_t<F, T&>> transform(F&& f) const;241 template<class F> constexpr optional or_else(F&& f) const;242 243 // [optional.ref.mod], modifiers244 constexpr void reset() noexcept;245 246 private:247 T* val = nullptr; // exposition only248 249 // [optional.ref.expos], exposition only helper functions250 template<class U>251 constexpr void convert-ref-init-val(U&& u); // exposition only252 };253 254} // namespace std255 256*/257 258#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)259# include <__cxx03/__config>260#else261# include <__assert>262# include <__compare/compare_three_way_result.h>263# include <__compare/ordering.h>264# include <__compare/three_way_comparable.h>265# include <__concepts/invocable.h>266# include <__config>267# include <__cstddef/ptrdiff_t.h>268# include <__exception/exception.h>269# include <__format/range_format.h>270# include <__functional/hash.h>271# include <__functional/invoke.h>272# include <__functional/unary_function.h>273# include <__fwd/functional.h>274# include <__iterator/bounded_iter.h>275# include <__iterator/wrap_iter.h>276# include <__memory/addressof.h>277# include <__memory/construct_at.h>278# include <__ranges/enable_borrowed_range.h>279# include <__ranges/enable_view.h>280# include <__tuple/sfinae_helpers.h>281# include <__type_traits/add_pointer.h>282# include <__type_traits/conditional.h>283# include <__type_traits/conjunction.h>284# include <__type_traits/decay.h>285# include <__type_traits/disjunction.h>286# include <__type_traits/enable_if.h>287# include <__type_traits/invoke.h>288# include <__type_traits/is_array.h>289# include <__type_traits/is_assignable.h>290# include <__type_traits/is_constructible.h>291# include <__type_traits/is_convertible.h>292# include <__type_traits/is_core_convertible.h>293# include <__type_traits/is_destructible.h>294# include <__type_traits/is_function.h>295# include <__type_traits/is_nothrow_assignable.h>296# include <__type_traits/is_nothrow_constructible.h>297# include <__type_traits/is_object.h>298# include <__type_traits/is_reference.h>299# include <__type_traits/is_same.h>300# include <__type_traits/is_scalar.h>301# include <__type_traits/is_swappable.h>302# include <__type_traits/is_trivially_assignable.h>303# include <__type_traits/is_trivially_constructible.h>304# include <__type_traits/is_trivially_destructible.h>305# include <__type_traits/is_trivially_relocatable.h>306# include <__type_traits/negation.h>307# include <__type_traits/reference_constructs_from_temporary.h>308# include <__type_traits/remove_const.h>309# include <__type_traits/remove_cv.h>310# include <__type_traits/remove_cvref.h>311# include <__type_traits/remove_reference.h>312# include <__utility/declval.h>313# include <__utility/forward.h>314# include <__utility/in_place.h>315# include <__utility/move.h>316# include <__utility/swap.h>317# include <__verbose_abort>318# include <initializer_list>319# include <version>320 321// standard-mandated includes322 323// [optional.syn]324# include <compare>325 326# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)327# pragma GCC system_header328# endif329 330_LIBCPP_PUSH_MACROS331# include <__undef_macros>332 333namespace std // purposefully not using versioning namespace334{335 336class _LIBCPP_EXPORTED_FROM_ABI bad_optional_access : public exception {337public:338 _LIBCPP_HIDE_FROM_ABI bad_optional_access() _NOEXCEPT = default;339 _LIBCPP_HIDE_FROM_ABI bad_optional_access(const bad_optional_access&) _NOEXCEPT = default;340 _LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default;341 // Get the key function ~bad_optional_access() into the dylib342 ~bad_optional_access() _NOEXCEPT override;343 const char* what() const _NOEXCEPT override;344};345 346} // namespace std347 348# if _LIBCPP_STD_VER >= 17349 350_LIBCPP_BEGIN_NAMESPACE_STD351 352[[noreturn]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_optional_access() {353# if _LIBCPP_HAS_EXCEPTIONS354 throw bad_optional_access();355# else356 _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode");357# endif358}359 360struct nullopt_t {361 struct __secret_tag {362 explicit __secret_tag() = default;363 };364 _LIBCPP_HIDE_FROM_ABI constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}365};366 367inline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};368 369struct __optional_construct_from_invoke_tag {};370 371template <class _Tp, bool = is_trivially_destructible<_Tp>::value>372struct __optional_destruct_base;373 374template <class _Tp>375struct __optional_destruct_base<_Tp, false> {376 typedef _Tp value_type;377 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");378 union {379 char __null_state_;380 remove_cv_t<value_type> __val_;381 };382 bool __engaged_;383 384 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__optional_destruct_base() {385 if (__engaged_)386 __val_.~value_type();387 }388 389 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}390 391 template <class... _Args>392 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)393 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}394 395# if _LIBCPP_STD_VER >= 23396 template <class _Fp, class... _Args>397 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(398 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)399 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}400# endif401 402 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {403 if (__engaged_) {404 __val_.~value_type();405 __engaged_ = false;406 }407 }408};409 410template <class _Tp>411struct __optional_destruct_base<_Tp, true> {412 typedef _Tp value_type;413 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");414 union {415 char __null_state_;416 remove_cv_t<value_type> __val_;417 };418 bool __engaged_;419 420 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}421 422 template <class... _Args>423 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)424 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}425 426# if _LIBCPP_STD_VER >= 23427 template <class _Fp, class... _Args>428 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base(429 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)430 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}431# endif432 433 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {434 if (__engaged_) {435 __engaged_ = false;436 }437 }438};439 440template <class _Tp, bool = is_reference<_Tp>::value>441struct __optional_storage_base : __optional_destruct_base<_Tp> {442 using __base _LIBCPP_NODEBUG = __optional_destruct_base<_Tp>;443 using value_type = _Tp;444 using __base::__base;445 446 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; }447 448 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; }449 _LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; }450 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() && noexcept { return std::move(this->__val_); }451 _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& __get() const&& noexcept { return std::move(this->__val_); }452 453 template <class... _Args>454 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_Args&&... __args) {455 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");456 std::__construct_at(std::addressof(this->__val_), std::forward<_Args>(__args)...);457 this->__engaged_ = true;458 }459 460 template <class _That>461 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {462 if (__opt.has_value())463 __construct(std::forward<_That>(__opt).__get());464 }465 466 template <class _That>467 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {468 if (this->__engaged_ == __opt.has_value()) {469 if (this->__engaged_)470 static_cast<_Tp&>(this->__val_) = std::forward<_That>(__opt).__get();471 } else {472 if (this->__engaged_)473 this->reset();474 else475 __construct(std::forward<_That>(__opt).__get());476 }477 }478 479 template <class _Up>480 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from_val(_Up&& __val) {481 this->__get() = std::forward<_Up>(__val);482 }483 484 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__optional_storage_base& __rhs) {485 using std::swap;486 swap(this->__get(), __rhs.__get());487 }488};489 490template <class _Tp>491struct __optional_storage_base<_Tp, true> {492 using value_type = _Tp;493 using __raw_type _LIBCPP_NODEBUG = remove_reference_t<_Tp>;494 __raw_type* __value_;495 496 _LIBCPP_HIDE_FROM_ABI constexpr __optional_storage_base() noexcept : __value_(nullptr) {}497 498 template <class _UArg>499 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg)500 : __value_(std::addressof(__uarg)) {501 static_assert(!__reference_constructs_from_temporary_v<_Tp, _UArg>,502 "Attempted to construct a reference element in tuple from a "503 "possible temporary");504 }505 506 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; }507 508 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; }509 510 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const& noexcept { return *__value_; }511 512 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() const&& noexcept { return std::forward<value_type>(*__value_); }513 514 template <class _UArg>515 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_UArg&& __val) {516 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");517 static_assert(!__reference_constructs_from_temporary_v<_Tp, _UArg>,518 "Attempted to construct a reference element in tuple from a "519 "possible temporary");520 __value_ = std::addressof(__val);521 }522 523 template <class _That>524 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {525 if (__opt.has_value())526 __construct(std::forward<_That>(__opt).__get());527 }528 529 template <class _That>530 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {531 if (has_value() == __opt.has_value()) {532 if (has_value())533 *__value_ = std::forward<_That>(__opt).__get();534 } else {535 if (has_value())536 reset();537 else538 __construct(std::forward<_That>(__opt).__get());539 }540 }541 542 template <class _Up>543 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from_val(_Up&& __val) noexcept {544 __value_ = std::addressof(__val);545 }546 547 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__optional_storage_base& __rhs) noexcept {548 std::swap(__value_, __rhs.__value_);549 }550};551 552template <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value>553struct __optional_copy_base : __optional_storage_base<_Tp> {554 using __optional_storage_base<_Tp>::__optional_storage_base;555};556 557template <class _Tp>558struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> {559 using __optional_storage_base<_Tp>::__optional_storage_base;560 561 _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default;562 563 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) {564 this->__construct_from(__opt);565 }566 567 _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&) = default;568 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default;569 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&) = default;570};571 572template <class _Tp, bool = is_trivially_move_constructible<_Tp>::value>573struct __optional_move_base : __optional_copy_base<_Tp> {574 using __optional_copy_base<_Tp>::__optional_copy_base;575};576 577template <class _Tp>578struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> {579 using value_type = _Tp;580 using __optional_copy_base<_Tp>::__optional_copy_base;581 582 _LIBCPP_HIDE_FROM_ABI __optional_move_base() = default;583 _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default;584 585 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20586 __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) {587 this->__construct_from(std::move(__opt));588 }589 590 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default;591 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&) = default;592};593 594template <class _Tp,595 bool = is_trivially_destructible<_Tp>::value && is_trivially_copy_constructible<_Tp>::value &&596 is_trivially_copy_assignable<_Tp>::value>597struct __optional_copy_assign_base : __optional_move_base<_Tp> {598 using __optional_move_base<_Tp>::__optional_move_base;599};600 601template <class _Tp>602struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> {603 using __optional_move_base<_Tp>::__optional_move_base;604 605 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base() = default;606 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default;607 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&) = default;608 609 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base&610 operator=(const __optional_copy_assign_base& __opt) {611 this->__assign_from(__opt);612 return *this;613 }614 615 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;616};617 618template <class _Tp,619 bool = is_trivially_destructible<_Tp>::value && is_trivially_move_constructible<_Tp>::value &&620 is_trivially_move_assignable<_Tp>::value>621struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> {622 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;623};624 625template <class _Tp>626struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> {627 using value_type = _Tp;628 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;629 630 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base() = default;631 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;632 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&) = default;633 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;634 635 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base&636 operator=(__optional_move_assign_base&& __opt) noexcept(637 is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) {638 this->__assign_from(std::move(__opt));639 return *this;640 }641};642 643template <class _Tp>644using __optional_sfinae_ctor_base_t _LIBCPP_NODEBUG =645 __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >;646 647template <class _Tp>648using __optional_sfinae_assign_base_t _LIBCPP_NODEBUG =649 __sfinae_assign_base< (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),650 (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value) >;651 652template <class _Tp>653class optional;654 655# if _LIBCPP_STD_VER >= 26656template <class _Tp>657constexpr bool ranges::enable_view<optional<_Tp>> = true;658 659template <class _Tp>660constexpr range_format format_kind<optional<_Tp>> = range_format::disabled;661 662template <class _Tp>663constexpr bool ranges::enable_borrowed_range<optional<_Tp&>> = true;664 665# endif666 667# if _LIBCPP_STD_VER >= 20668 669template <class _Tp>670concept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(const optional<_Up>&) {}(__t); };671 672# endif // _LIBCPP_STD_VER >= 20673 674template <class _Tp>675struct __is_std_optional : false_type {};676template <class _Tp>677struct __is_std_optional<optional<_Tp>> : true_type {};678 679template <class _Tp, class = void>680struct __optional_iterator {};681 682template <class _Tp>683struct __optional_iterator<684 _Tp,685 enable_if_t<!(is_lvalue_reference_v<_Tp> && is_function_v<__libcpp_remove_reference_t<_Tp>>) &&686 !(is_lvalue_reference_v<_Tp> && is_array_v<__libcpp_remove_reference_t<_Tp>>)> > {687private:688 using __pointer _LIBCPP_NODEBUG = add_pointer_t<remove_reference_t<_Tp>>;689 using __const_pointer _LIBCPP_NODEBUG = add_pointer_t<const remove_reference_t<_Tp>>;690 691public:692# if _LIBCPP_STD_VER >= 26693# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL694 using iterator = __bounded_iter<__wrap_iter<__pointer>>;695 using const_iterator = __bounded_iter<__wrap_iter<__const_pointer>>;696# else697 using iterator = __wrap_iter<__pointer>;698 using const_iterator = __wrap_iter<__const_pointer>;699# endif700 701 // [optional.iterators], iterator support702 _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {703 auto& __derived_self = static_cast<optional<_Tp>&>(*this);704 auto __ptr = [&__derived_self]() {705 if constexpr (is_lvalue_reference_v<_Tp>) {706 return __derived_self.has_value() ? std::addressof(__derived_self.__get()) : nullptr;707 }708 return std::addressof(__derived_self.__get());709 }();710 711# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL712 return std::__make_bounded_iter(713 __wrap_iter<__pointer>(__ptr),714 __wrap_iter<__pointer>(__ptr),715 __wrap_iter<__pointer>(__ptr) + (__derived_self.has_value() ? 1 : 0));716# else717 return iterator(__ptr);718# endif719 }720 721 _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {722 auto& __derived_self = static_cast<const optional<_Tp>&>(*this);723 auto* __ptr = [&__derived_self]() {724 if constexpr (is_lvalue_reference_v<_Tp>) {725 return __derived_self.has_value() ? std::addressof(__derived_self.__get()) : nullptr;726 }727 return std::addressof(__derived_self.__get());728 }();729 730# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL731 return std::__make_bounded_iter(732 __wrap_iter<__const_pointer>(__ptr),733 __wrap_iter<__const_pointer>(__ptr),734 __wrap_iter<__const_pointer>(__ptr) + (__derived_self.has_value() ? 1 : 0));735# else736 return const_iterator(__ptr);737# endif738 }739 740 _LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept {741 return begin() + (static_cast<optional<_Tp>&>(*this).has_value() ? 1 : 0);742 }743 _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept {744 return begin() + (static_cast<const optional<_Tp>&>(*this).has_value() ? 1 : 0);745 }746# endif747};748 749template <class _Tp>750class _LIBCPP_DECLSPEC_EMPTY_BASES optional751 : private __optional_move_assign_base<_Tp>,752 private __optional_sfinae_ctor_base_t<_Tp>,753 private __optional_sfinae_assign_base_t<_Tp>,754 public __optional_iterator<_Tp> {755 using __base _LIBCPP_NODEBUG = __optional_move_assign_base<_Tp>;756 757public:758 using value_type = __libcpp_remove_reference_t<_Tp>;759 760 using __trivially_relocatable _LIBCPP_NODEBUG =761 conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, optional, void>;762 763private:764 static_assert(!is_same_v<__remove_cvref_t<_Tp>, in_place_t>,765 "instantiation of optional with in_place_t is ill-formed");766 static_assert(!is_same_v<__remove_cvref_t<_Tp>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed");767# if _LIBCPP_STD_VER >= 26768 static_assert(!is_rvalue_reference_v<_Tp>, "instantiation of optional with an rvalue reference type is ill-formed");769# else770 static_assert(!is_reference_v<_Tp>, "instantiation of optional with a reference type is ill-formed");771# endif772 static_assert(is_destructible_v<_Tp>, "instantiation of optional with a non-destructible type is ill-formed");773 static_assert(!is_array_v<_Tp>, "instantiation of optional with an array type is ill-formed");774 775# if _LIBCPP_STD_VER >= 26776 template <class _Up>777 constexpr static bool __libcpp_opt_ref_ctor_deleted =778 is_lvalue_reference_v<_Tp> && reference_constructs_from_temporary_v<_Tp, _Up>;779# endif780 781 // LWG2756: conditionally explicit conversion from _Up782 struct _CheckOptionalArgsConstructor {783 template <class _Up>784 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {785 return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>;786 }787 788 template <class _Up>789 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {790 return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>;791 }792 };793 template <class _Up>794 using _CheckOptionalArgsCtor _LIBCPP_NODEBUG =795 _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value &&796 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value),797 _CheckOptionalArgsConstructor,798 __check_tuple_constructor_fail >;799 template <class _QualUp>800 struct _CheckOptionalLikeConstructor {801 template <class _Up, class _Opt = optional<_Up>>802 using __check_constructible_from_opt _LIBCPP_NODEBUG =803 _Or< is_constructible<_Tp, _Opt&>,804 is_constructible<_Tp, _Opt const&>,805 is_constructible<_Tp, _Opt&&>,806 is_constructible<_Tp, _Opt const&&>,807 is_convertible<_Opt&, _Tp>,808 is_convertible<_Opt const&, _Tp>,809 is_convertible<_Opt&&, _Tp>,810 is_convertible<_Opt const&&, _Tp> >;811 template <class _Up, class _Opt = optional<_Up>>812 using __check_assignable_from_opt _LIBCPP_NODEBUG =813 _Or< is_assignable<_Tp&, _Opt&>,814 is_assignable<_Tp&, _Opt const&>,815 is_assignable<_Tp&, _Opt&&>,816 is_assignable<_Tp&, _Opt const&&> >;817 template <class _Up, class _QUp = _QualUp>818 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {819 return is_convertible<_QUp, _Tp>::value &&820 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);821 }822 template <class _Up, class _QUp = _QualUp>823 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {824 return !is_convertible<_QUp, _Tp>::value &&825 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);826 }827 template <class _Up, class _QUp = _QualUp>828 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() {829 // Construction and assignability of _QUp to _Tp has already been830 // checked.831 return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value;832 }833 };834 835 template <class _Up, class _QualUp>836 using _CheckOptionalLikeCtor _LIBCPP_NODEBUG =837 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value,838 _CheckOptionalLikeConstructor<_QualUp>,839 __check_tuple_constructor_fail >;840 template <class _Up, class _QualUp>841 using _CheckOptionalLikeAssign _LIBCPP_NODEBUG =842 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value,843 _CheckOptionalLikeConstructor<_QualUp>,844 __check_tuple_constructor_fail >;845 846public:847 _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {}848 _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default;849 _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&) = default;850 _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {}851 852 template <class _InPlaceT,853 class... _Args,854 enable_if_t<_And<_IsSame<_InPlaceT, in_place_t>, is_constructible<_Tp, _Args...>>::value, int> = 0>855 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args)856 : __base(in_place, std::forward<_Args>(__args)...) {}857 858 template <class _Up, class... _Args, enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>859 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)860 : __base(in_place, __il, std::forward<_Args>(__args)...) {}861 862 template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>863 _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}864 865 template <class _Up = remove_cv_t<_Tp>,866 enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>867 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}868 869 // LWG2756: conditionally explicit conversion from const optional<_Up>&870 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>871 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v) {872 this->__construct_from(__v);873 }874 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>875 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v) {876 this->__construct_from(__v);877 }878 879 // LWG2756: conditionally explicit conversion from optional<_Up>&&880 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>881 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v) {882 this->__construct_from(std::move(__v));883 }884 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>885 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v) {886 this->__construct_from(std::move(__v));887 }888 889 // deleted optional<T&> constructors890# if _LIBCPP_STD_VER >= 26891 template <class _Up, class... _Args, enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>892 requires __libcpp_opt_ref_ctor_deleted<_Up>893 explicit optional(in_place_t, initializer_list<_Up>, _Args&&...) = delete;894 895 template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>896 requires __libcpp_opt_ref_ctor_deleted<_Up>897 optional(_Up&&) = delete;898 899 template <class _Up = remove_cv_t<_Tp>,900 enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>901 requires __libcpp_opt_ref_ctor_deleted<_Up>902 explicit optional(_Up&&) = delete;903 904 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>905 requires __libcpp_opt_ref_ctor_deleted<_Up>906 optional(const optional<_Up>&) = delete;907 908 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>909 requires __libcpp_opt_ref_ctor_deleted<_Up>910 explicit optional(const optional<_Up>&) = delete;911 912 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>913 requires __libcpp_opt_ref_ctor_deleted<_Up>914 optional(optional<_Up>&&) = delete;915 916 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>917 requires __libcpp_opt_ref_ctor_deleted<_Up>918 explicit optional(optional<_Up>&&) = delete;919# endif920 921# if _LIBCPP_STD_VER >= 23922 template <class _Tag,923 class _Fp,924 class... _Args,925 enable_if_t<_IsSame<_Tag, __optional_construct_from_invoke_tag>::value, int> = 0>926 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Tag, _Fp&& __f, _Args&&... __args)927 : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}928# endif929 930 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept {931 reset();932 return *this;933 }934 935 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default;936 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default;937 938 // LWG2756939 template <class _Up = remove_cv_t<_Tp>,940 enable_if_t<_And<_IsNotSame<__remove_cvref_t<_Up>, optional>,941 _Or<_IsNotSame<__remove_cvref_t<_Up>, _Tp>, _Not<is_scalar<_Tp>>>,942 is_constructible<_Tp, _Up>,943 is_assignable<_Tp&, _Up>>::value,944 int> = 0>945 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) {946 if (this->has_value())947 this->__assign_from_val(std::forward<_Up>(__v));948 else949 this->__construct(std::forward<_Up>(__v));950 return *this;951 }952 953 // LWG2756954 template <class _Up, enable_if_t<_CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0>955 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) {956 this->__assign_from(__v);957 return *this;958 }959 960 // LWG2756961 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0>962 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) {963 this->__assign_from(std::move(__v));964 return *this;965 }966 967 template <class... _Args, enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>968 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {969 reset();970 this->__construct(std::forward<_Args>(__args)...);971 return this->__get();972 }973 974 template <class _Up,975 class... _Args,976 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>977# if _LIBCPP_STD_VER >= 26978 && !reference_constructs_from_temporary_v<_Tp&, _Up>979# endif980 ,981 int> = 0>982 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {983 reset();984 this->__construct(__il, std::forward<_Args>(__args)...);985 return this->__get();986 }987 988 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void989 swap(optional& __opt) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>) {990 if (this->has_value() == __opt.has_value()) {991 if (this->has_value())992 this->__swap(__opt);993 } else {994 if (this->has_value()) {995 __opt.__construct(std::move(this->__get()));996 reset();997 } else {998 this->__construct(std::move(__opt.__get()));999 __opt.reset();1000 }1001 }1002 }1003 1004 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp const> operator->() const noexcept {1005 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");1006 return std::addressof(this->__get());1007 }1008 1009 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() noexcept {1010 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");1011 return std::addressof(this->__get());1012 }1013 1014 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {1015 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");1016 return this->__get();1017 }1018 1019 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {1020 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");1021 return this->__get();1022 }1023 1024 _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {1025 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");1026 return std::move(this->__get());1027 }1028 1029 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {1030 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");1031 return std::move(this->__get());1032 }1033 1034 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); }1035 1036 using __base::__get;1037 using __base::has_value;1038 1039 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& {1040 if (!this->has_value())1041 std::__throw_bad_optional_access();1042 return this->__get();1043 }1044 1045 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {1046 if (!this->has_value())1047 std::__throw_bad_optional_access();1048 return this->__get();1049 }1050 1051 _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {1052 if (!this->has_value())1053 std::__throw_bad_optional_access();1054 return std::move(this->__get());1055 }1056 1057 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& {1058 if (!this->has_value())1059 std::__throw_bad_optional_access();1060 return std::move(this->__get());1061 }1062 1063 template <class _Up = remove_cv_t<_Tp>>1064# if _LIBCPP_STD_VER >= 261065 requires(!(is_lvalue_reference_v<_Tp> && is_function_v<__libcpp_remove_reference_t<_Tp>>) &&1066 !(is_lvalue_reference_v<_Tp> && is_array_v<__libcpp_remove_reference_t<_Tp>>))1067# endif1068 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {1069 static_assert(is_copy_constructible_v<_Tp>, "optional<T>::value_or: T must be copy constructible");1070 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");1071 return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));1072 }1073 1074 template <class _Up = remove_cv_t<_Tp>>1075# if _LIBCPP_STD_VER >= 261076 requires(!is_lvalue_reference_v<_Tp>)1077# endif1078 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {1079 static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");1080 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");1081 return this->has_value() ? std::move(this->__get()) : static_cast<_Tp>(std::forward<_Up>(__v));1082 }1083 1084# if _LIBCPP_STD_VER >= 261085 template <class _Up = remove_cv_t<_Tp>>1086 requires(is_lvalue_reference_v<_Tp> &&1087 !(is_function_v<__libcpp_remove_reference_t<_Tp>> || is_array_v<__libcpp_remove_reference_t<_Tp>>))1088 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {1089 static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");1090 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");1091 return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));1092 }1093# endif1094 1095# if _LIBCPP_STD_VER >= 231096 template <class _Func>1097 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {1098 using _Up = invoke_result_t<_Func, _Tp&>;1099 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,1100 "Result of f(value()) must be a specialization of std::optional");1101 if (*this)1102 return std::invoke(std::forward<_Func>(__f), value());1103 return remove_cvref_t<_Up>();1104 }1105 1106 template <class _Func>1107 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {1108 using _Up = invoke_result_t<_Func, const _Tp&>;1109 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,1110 "Result of f(value()) must be a specialization of std::optional");1111 if (*this)1112 return std::invoke(std::forward<_Func>(__f), value());1113 return remove_cvref_t<_Up>();1114 }1115 1116 template <class _Func>1117 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {1118 using _Up = invoke_result_t<_Func, _Tp&&>;1119 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,1120 "Result of f(std::move(value())) must be a specialization of std::optional");1121 if (*this)1122 return std::invoke(std::forward<_Func>(__f), std::move(value()));1123 return remove_cvref_t<_Up>();1124 }1125 1126 template <class _Func>1127 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {1128 using _Up = invoke_result_t<_Func, const _Tp&&>;1129 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,1130 "Result of f(std::move(value())) must be a specialization of std::optional");1131 if (*this)1132 return std::invoke(std::forward<_Func>(__f), std::move(value()));1133 return remove_cvref_t<_Up>();1134 }1135 1136 template <class _Func>1137 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {1138 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;1139 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");1140 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");1141 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");1142 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");1143 if (*this)1144 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());1145 return optional<_Up>();1146 }1147 1148 template <class _Func>1149 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {1150 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;1151 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");1152 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");1153 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");1154 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");1155 if (*this)1156 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());1157 return optional<_Up>();1158 }1159 1160 template <class _Func>1161 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {1162 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;1163 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");1164 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");1165 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");1166 static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");1167 if (*this)1168 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));1169 return optional<_Up>();1170 }1171 1172 template <class _Func>1173 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {1174 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;1175 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");1176 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");1177 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");1178 static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");1179 if (*this)1180 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));1181 return optional<_Up>();1182 }1183 1184 template <invocable _Func>1185 _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&1186 requires is_copy_constructible_v<_Tp>1187 {1188 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,1189 "Result of f() should be the same type as this optional");1190 if (*this)1191 return *this;1192 return std::forward<_Func>(__f)();1193 }1194 1195 template <invocable _Func>1196 _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&1197 requires is_move_constructible_v<_Tp>1198 {1199 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,1200 "Result of f() should be the same type as this optional");1201 if (*this)1202 return std::move(*this);1203 return std::forward<_Func>(__f)();1204 }1205# endif // _LIBCPP_STD_VER >= 231206 1207 using __base::reset;1208};1209 1210template <class _Tp>1211optional(_Tp) -> optional<_Tp>;1212 1213// [optional.relops] Relational operators1214 1215template <1216 class _Tp,1217 class _Up,1218 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,1219 int> = 0>1220_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, const optional<_Up>& __y) {1221 if (static_cast<bool>(__x) != static_cast<bool>(__y))1222 return false;1223 if (!static_cast<bool>(__x))1224 return true;1225 return *__x == *__y;1226}1227 1228template <1229 class _Tp,1230 class _Up,1231 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,1232 int> = 0>1233_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) {1234 if (static_cast<bool>(__x) != static_cast<bool>(__y))1235 return true;1236 if (!static_cast<bool>(__x))1237 return false;1238 return *__x != *__y;1239}1240 1241template < class _Tp,1242 class _Up,1243 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,1244 int> = 0>1245_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>& __x, const optional<_Up>& __y) {1246 if (!static_cast<bool>(__y))1247 return false;1248 if (!static_cast<bool>(__x))1249 return true;1250 return *__x < *__y;1251}1252 1253template < class _Tp,1254 class _Up,1255 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,1256 int> = 0>1257_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, const optional<_Up>& __y) {1258 if (!static_cast<bool>(__x))1259 return false;1260 if (!static_cast<bool>(__y))1261 return true;1262 return *__x > *__y;1263}1264 1265template <1266 class _Tp,1267 class _Up,1268 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,1269 int> = 0>1270_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, const optional<_Up>& __y) {1271 if (!static_cast<bool>(__x))1272 return true;1273 if (!static_cast<bool>(__y))1274 return false;1275 return *__x <= *__y;1276}1277 1278template <1279 class _Tp,1280 class _Up,1281 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,1282 int> = 0>1283_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) {1284 if (!static_cast<bool>(__y))1285 return true;1286 if (!static_cast<bool>(__x))1287 return false;1288 return *__x >= *__y;1289}1290 1291# if _LIBCPP_STD_VER >= 201292 1293template <class _Tp, three_way_comparable_with<_Tp> _Up>1294_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>1295operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) {1296 if (__x && __y)1297 return *__x <=> *__y;1298 return __x.has_value() <=> __y.has_value();1299}1300 1301# endif // _LIBCPP_STD_VER >= 201302 1303// [optional.nullops] Comparison with nullopt1304 1305template <class _Tp>1306_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept {1307 return !static_cast<bool>(__x);1308}1309 1310# if _LIBCPP_STD_VER <= 171311 1312template <class _Tp>1313_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(nullopt_t, const optional<_Tp>& __x) noexcept {1314 return !static_cast<bool>(__x);1315}1316 1317template <class _Tp>1318_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, nullopt_t) noexcept {1319 return static_cast<bool>(__x);1320}1321 1322template <class _Tp>1323_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(nullopt_t, const optional<_Tp>& __x) noexcept {1324 return static_cast<bool>(__x);1325}1326 1327template <class _Tp>1328_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>&, nullopt_t) noexcept {1329 return false;1330}1331 1332template <class _Tp>1333_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(nullopt_t, const optional<_Tp>& __x) noexcept {1334 return static_cast<bool>(__x);1335}1336 1337template <class _Tp>1338_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, nullopt_t) noexcept {1339 return !static_cast<bool>(__x);1340}1341 1342template <class _Tp>1343_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(nullopt_t, const optional<_Tp>&) noexcept {1344 return true;1345}1346 1347template <class _Tp>1348_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, nullopt_t) noexcept {1349 return static_cast<bool>(__x);1350}1351 1352template <class _Tp>1353_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(nullopt_t, const optional<_Tp>&) noexcept {1354 return false;1355}1356 1357template <class _Tp>1358_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>&, nullopt_t) noexcept {1359 return true;1360}1361 1362template <class _Tp>1363_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(nullopt_t, const optional<_Tp>& __x) noexcept {1364 return !static_cast<bool>(__x);1365}1366 1367# else // _LIBCPP_STD_VER <= 171368 1369template <class _Tp>1370_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept {1371 return __x.has_value() <=> false;1372}1373 1374# endif // _LIBCPP_STD_VER <= 171375 1376// [optional.comp.with.t] Comparison with T1377 1378template <1379 class _Tp,1380 class _Up,1381 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,1382 int> = 0>1383_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, const _Up& __v) {1384 return static_cast<bool>(__x) ? *__x == __v : false;1385}1386 1387template <1388 class _Tp,1389 class _Up,1390 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,1391 int> = 0>1392_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const _Tp& __v, const optional<_Up>& __x) {1393 return static_cast<bool>(__x) ? __v == *__x : false;1394}1395 1396template <1397 class _Tp,1398 class _Up,1399 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,1400 int> = 0>1401_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, const _Up& __v) {1402 return static_cast<bool>(__x) ? *__x != __v : true;1403}1404 1405template <1406 class _Tp,1407 class _Up,1408 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,1409 int> = 0>1410_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const _Tp& __v, const optional<_Up>& __x) {1411 return static_cast<bool>(__x) ? __v != *__x : true;1412}1413 1414template < class _Tp,1415 class _Up,1416 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,1417 int> = 0>1418_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>& __x, const _Up& __v) {1419 return static_cast<bool>(__x) ? *__x < __v : true;1420}1421 1422template < class _Tp,1423 class _Up,1424 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,1425 int> = 0>1426_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const _Tp& __v, const optional<_Up>& __x) {1427 return static_cast<bool>(__x) ? __v < *__x : false;1428}1429 1430template <1431 class _Tp,1432 class _Up,1433 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,1434 int> = 0>1435_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, const _Up& __v) {1436 return static_cast<bool>(__x) ? *__x <= __v : true;1437}1438 1439template <1440 class _Tp,1441 class _Up,1442 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,1443 int> = 0>1444_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const _Tp& __v, const optional<_Up>& __x) {1445 return static_cast<bool>(__x) ? __v <= *__x : false;1446}1447 1448template < class _Tp,1449 class _Up,1450 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,1451 int> = 0>1452_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, const _Up& __v) {1453 return static_cast<bool>(__x) ? *__x > __v : false;1454}1455 1456template < class _Tp,1457 class _Up,1458 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,1459 int> = 0>1460_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const _Tp& __v, const optional<_Up>& __x) {1461 return static_cast<bool>(__x) ? __v > *__x : true;1462}1463 1464template <1465 class _Tp,1466 class _Up,1467 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,1468 int> = 0>1469_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>& __x, const _Up& __v) {1470 return static_cast<bool>(__x) ? *__x >= __v : false;1471}1472 1473template <1474 class _Tp,1475 class _Up,1476 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,1477 int> = 0>1478_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const _Tp& __v, const optional<_Up>& __x) {1479 return static_cast<bool>(__x) ? __v >= *__x : true;1480}1481 1482# if _LIBCPP_STD_VER >= 201483 1484template <class _Tp, class _Up>1485 requires(!__is_derived_from_optional<_Up>) && three_way_comparable_with<_Tp, _Up>1486_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>1487operator<=>(const optional<_Tp>& __x, const _Up& __v) {1488 return __x.has_value() ? *__x <=> __v : strong_ordering::less;1489}1490 1491# endif // _LIBCPP_STD_VER >= 201492 1493template <class _Tp, enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, int> = 0>1494inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void1495swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {1496 __x.swap(__y);1497}1498 1499struct __make_optional_barrier_tag {1500 explicit __make_optional_barrier_tag() = default;1501};1502 1503template <1504# if _LIBCPP_STD_VER >= 261505 __make_optional_barrier_tag = __make_optional_barrier_tag{},1506# endif1507 class _Tp>1508_LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {1509 return optional<decay_t<_Tp>>(std::forward<_Tp>(__v));1510}1511 1512template <class _Tp, class... _Args>1513_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {1514 return optional<_Tp>(in_place, std::forward<_Args>(__args)...);1515}1516 1517template <class _Tp, class _Up, class... _Args>1518_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) {1519 return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);1520}1521 1522template <class _Tp>1523struct hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > {1524# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)1525 _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type;1526 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;1527# endif1528 1529 _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {1530 return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;1531 }1532};1533 1534_LIBCPP_END_NAMESPACE_STD1535 1536# endif // _LIBCPP_STD_VER >= 171537 1538_LIBCPP_POP_MACROS1539 1540# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 201541# include <atomic>1542# include <climits>1543# include <concepts>1544# include <ctime>1545# include <iterator>1546# include <limits>1547# include <memory>1548# include <ratio>1549# include <stdexcept>1550# include <tuple>1551# include <type_traits>1552# include <typeinfo>1553# include <utility>1554# include <variant>1555# endif1556#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)1557 1558#endif // _LIBCPP_OPTIONAL1559