2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_OPTIONAL
11 #define _LIBCPP_OPTIONAL
19 // [optional.optional], class template optional
24 concept is-derived-from-optional = requires(const T& t) { // exposition only
25 []<class U>(const optional<U>&){ }(t);
28 // [optional.nullopt], no-value state indicator
29 struct nullopt_t{see below };
30 inline constexpr nullopt_t nullopt(unspecified );
32 // [optional.bad.access], class bad_optional_access
33 class bad_optional_access;
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
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
66 constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // since C++20
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
86 // [optional.specalg], specialized algorithms
88 void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20
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);
97 // [optional.hash], hash support
98 template<class T> struct hash;
99 template<class T> struct hash<optional<T>>;
104 using value_type = T;
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 &&);
118 explicit(see-below) optional(const optional<U> &); // constexpr in C++20
120 explicit(see-below) optional(optional<U> &&); // constexpr in C++20
122 // [optional.dtor], destructor
123 ~optional(); // constexpr in C++20
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
135 // [optional.swap], swap
136 void swap(optional &) noexcept(see below ); // constexpr in C++20
138 // [optional.observe], observers
139 constexpr T const *operator->() const noexcept;
140 constexpr T *operator->() noexcept;
141 constexpr T const &operator*() const & noexcept;
142 constexpr T &operator*() & noexcept;
143 constexpr T &&operator*() && noexcept;
144 constexpr const T &&operator*() const && noexcept;
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 &&) &&;
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
166 // [optional.mod], modifiers
167 void reset() noexcept; // constexpr in C++20
170 T *val; // exposition only
174 optional(T) -> optional<T>;
181 #include <__compare/compare_three_way_result.h>
182 #include <__compare/ordering.h>
183 #include <__compare/three_way_comparable.h>
184 #include <__concepts/invocable.h>
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/enable_if.h>
200 #include <__type_traits/invoke.h>
201 #include <__type_traits/is_array.h>
202 #include <__type_traits/is_assignable.h>
203 #include <__type_traits/is_constructible.h>
204 #include <__type_traits/is_convertible.h>
205 #include <__type_traits/is_destructible.h>
206 #include <__type_traits/is_nothrow_assignable.h>
207 #include <__type_traits/is_nothrow_constructible.h>
208 #include <__type_traits/is_object.h>
209 #include <__type_traits/is_reference.h>
210 #include <__type_traits/is_same.h>
211 #include <__type_traits/is_scalar.h>
212 #include <__type_traits/is_swappable.h>
213 #include <__type_traits/is_trivially_assignable.h>
214 #include <__type_traits/is_trivially_constructible.h>
215 #include <__type_traits/is_trivially_destructible.h>
216 #include <__type_traits/is_trivially_relocatable.h>
217 #include <__type_traits/negation.h>
218 #include <__type_traits/remove_const.h>
219 #include <__type_traits/remove_cv.h>
220 #include <__type_traits/remove_cvref.h>
221 #include <__type_traits/remove_reference.h>
222 #include <__utility/declval.h>
223 #include <__utility/forward.h>
224 #include <__utility/in_place.h>
225 #include <__utility/move.h>
226 #include <__utility/swap.h>
227 #include <__verbose_abort>
228 #include <initializer_list>
232 // standard-mandated includes
237 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
238 # pragma GCC system_header
242 #include <__undef_macros>
244 namespace std // purposefully not using versioning namespace
247 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access : public exception {
249 _LIBCPP_HIDE_FROM_ABI bad_optional_access() _NOEXCEPT = default;
250 _LIBCPP_HIDE_FROM_ABI bad_optional_access(const bad_optional_access&) _NOEXCEPT = default;
251 _LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default;
252 // Get the key function ~bad_optional_access() into the dylib
253 ~bad_optional_access() _NOEXCEPT override;
254 const char* what() const _NOEXCEPT override;
259 #if _LIBCPP_STD_VER >= 17
261 _LIBCPP_BEGIN_NAMESPACE_STD
263 [[noreturn]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS void
264 __throw_bad_optional_access() {
265 # if _LIBCPP_HAS_EXCEPTIONS
266 throw bad_optional_access();
268 _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode");
273 struct __secret_tag {
274 explicit __secret_tag() = default;
276 _LIBCPP_HIDE_FROM_ABI constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
279 inline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
281 struct __optional_construct_from_invoke_tag {};
283 template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
284 struct __optional_destruct_base;
287 struct __optional_destruct_base<_Tp, false> {
288 typedef _Tp value_type;
289 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
292 remove_cv_t<value_type> __val_;
296 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__optional_destruct_base() {
298 __val_.~value_type();
301 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
303 template <class... _Args>
304 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
305 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
307 # if _LIBCPP_STD_VER >= 23
308 template <class _Fp, class... _Args>
309 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(
310 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
311 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
314 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
316 __val_.~value_type();
323 struct __optional_destruct_base<_Tp, true> {
324 typedef _Tp value_type;
325 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
328 remove_cv_t<value_type> __val_;
332 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
334 template <class... _Args>
335 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
336 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
338 # if _LIBCPP_STD_VER >= 23
339 template <class _Fp, class... _Args>
340 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base(
341 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
342 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
345 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
352 template <class _Tp, bool = is_reference<_Tp>::value>
353 struct __optional_storage_base : __optional_destruct_base<_Tp> {
354 using __base = __optional_destruct_base<_Tp>;
355 using value_type = _Tp;
356 using __base::__base;
358 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; }
360 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; }
361 _LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; }
362 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() && noexcept { return std::move(this->__val_); }
363 _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& __get() const&& noexcept { return std::move(this->__val_); }
365 template <class... _Args>
366 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_Args&&... __args) {
367 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
368 std::__construct_at(std::addressof(this->__val_), std::forward<_Args>(__args)...);
369 this->__engaged_ = true;
372 template <class _That>
373 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
374 if (__opt.has_value())
375 __construct(std::forward<_That>(__opt).__get());
378 template <class _That>
379 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
380 if (this->__engaged_ == __opt.has_value()) {
381 if (this->__engaged_)
382 static_cast<_Tp&>(this->__val_) = std::forward<_That>(__opt).__get();
384 if (this->__engaged_)
387 __construct(std::forward<_That>(__opt).__get());
392 // optional<T&> is currently required to be ill-formed. However, it may
393 // be allowed in the future. For this reason, it has already been implemented
394 // to ensure we can make the change in an ABI-compatible manner.
396 struct __optional_storage_base<_Tp, true> {
397 using value_type = _Tp;
398 using __raw_type = remove_reference_t<_Tp>;
399 __raw_type* __value_;
402 static _LIBCPP_HIDE_FROM_ABI constexpr bool __can_bind_reference() {
403 using _RawUp = __libcpp_remove_reference_t<_Up>;
404 using _UpPtr = _RawUp*;
405 using _RawTp = __libcpp_remove_reference_t<_Tp>;
406 using _TpPtr = _RawTp*;
407 using _CheckLValueArg =
408 integral_constant<bool,
409 (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value) ||
410 is_same<_RawUp, reference_wrapper<_RawTp>>::value ||
411 is_same<_RawUp, reference_wrapper<__remove_const_t<_RawTp>>>::value >;
412 return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value) ||
413 (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value &&
414 is_convertible<_UpPtr, _TpPtr>::value);
417 _LIBCPP_HIDE_FROM_ABI constexpr __optional_storage_base() noexcept : __value_(nullptr) {}
419 template <class _UArg>
420 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg)
421 : __value_(std::addressof(__uarg)) {
422 static_assert(__can_bind_reference<_UArg>(),
423 "Attempted to construct a reference element in tuple from a "
424 "possible temporary");
427 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; }
429 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; }
431 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const& noexcept { return *__value_; }
433 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() const&& noexcept { return std::forward<value_type>(*__value_); }
435 template <class _UArg>
436 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_UArg&& __val) {
437 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
438 static_assert(__can_bind_reference<_UArg>(),
439 "Attempted to construct a reference element in tuple from a "
440 "possible temporary");
441 __value_ = std::addressof(__val);
444 template <class _That>
445 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
446 if (__opt.has_value())
447 __construct(std::forward<_That>(__opt).__get());
450 template <class _That>
451 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
452 if (has_value() == __opt.has_value()) {
454 *__value_ = std::forward<_That>(__opt).__get();
459 __construct(std::forward<_That>(__opt).__get());
464 template <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value>
465 struct __optional_copy_base : __optional_storage_base<_Tp> {
466 using __optional_storage_base<_Tp>::__optional_storage_base;
470 struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> {
471 using __optional_storage_base<_Tp>::__optional_storage_base;
473 _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default;
475 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) {
476 this->__construct_from(__opt);
479 _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&) = default;
480 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default;
481 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&) = default;
484 template <class _Tp, bool = is_trivially_move_constructible<_Tp>::value>
485 struct __optional_move_base : __optional_copy_base<_Tp> {
486 using __optional_copy_base<_Tp>::__optional_copy_base;
490 struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> {
491 using value_type = _Tp;
492 using __optional_copy_base<_Tp>::__optional_copy_base;
494 _LIBCPP_HIDE_FROM_ABI __optional_move_base() = default;
495 _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default;
497 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
498 __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) {
499 this->__construct_from(std::move(__opt));
502 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default;
503 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&) = default;
507 bool = is_trivially_destructible<_Tp>::value && is_trivially_copy_constructible<_Tp>::value &&
508 is_trivially_copy_assignable<_Tp>::value>
509 struct __optional_copy_assign_base : __optional_move_base<_Tp> {
510 using __optional_move_base<_Tp>::__optional_move_base;
514 struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> {
515 using __optional_move_base<_Tp>::__optional_move_base;
517 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base() = default;
518 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
519 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&) = default;
521 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base&
522 operator=(const __optional_copy_assign_base& __opt) {
523 this->__assign_from(__opt);
527 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
531 bool = is_trivially_destructible<_Tp>::value && is_trivially_move_constructible<_Tp>::value &&
532 is_trivially_move_assignable<_Tp>::value>
533 struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> {
534 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
538 struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> {
539 using value_type = _Tp;
540 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
542 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base() = default;
543 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;
544 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&) = default;
545 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
547 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base&
548 operator=(__optional_move_assign_base&& __opt) noexcept(
549 is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) {
550 this->__assign_from(std::move(__opt));
556 using __optional_sfinae_ctor_base_t =
557 __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >;
560 using __optional_sfinae_assign_base_t =
561 __sfinae_assign_base< (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),
562 (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value) >;
567 # if _LIBCPP_STD_VER >= 20
570 concept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(const optional<_Up>&) {}(__t); };
572 # endif // _LIBCPP_STD_VER >= 20
575 struct __is_std_optional : false_type {};
577 struct __is_std_optional<optional<_Tp>> : true_type {};
580 class _LIBCPP_DECLSPEC_EMPTY_BASES optional
581 : private __optional_move_assign_base<_Tp>,
582 private __optional_sfinae_ctor_base_t<_Tp>,
583 private __optional_sfinae_assign_base_t<_Tp> {
584 using __base = __optional_move_assign_base<_Tp>;
587 using value_type = _Tp;
589 using __trivially_relocatable = conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, optional, void>;
592 // Disable the reference extension using this static assert.
593 static_assert(!is_same_v<__remove_cvref_t<value_type>, in_place_t>,
594 "instantiation of optional with in_place_t is ill-formed");
595 static_assert(!is_same_v<__remove_cvref_t<value_type>, nullopt_t>,
596 "instantiation of optional with nullopt_t is ill-formed");
597 static_assert(!is_reference_v<value_type>, "instantiation of optional with a reference type is ill-formed");
598 static_assert(is_destructible_v<value_type>, "instantiation of optional with a non-destructible type is ill-formed");
599 static_assert(!is_array_v<value_type>, "instantiation of optional with an array type is ill-formed");
601 // LWG2756: conditionally explicit conversion from _Up
602 struct _CheckOptionalArgsConstructor {
604 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
605 return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>;
609 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
610 return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>;
614 using _CheckOptionalArgsCtor =
615 _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value &&
616 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value),
617 _CheckOptionalArgsConstructor,
618 __check_tuple_constructor_fail >;
619 template <class _QualUp>
620 struct _CheckOptionalLikeConstructor {
621 template <class _Up, class _Opt = optional<_Up>>
622 using __check_constructible_from_opt =
623 _Or< is_constructible<_Tp, _Opt&>,
624 is_constructible<_Tp, _Opt const&>,
625 is_constructible<_Tp, _Opt&&>,
626 is_constructible<_Tp, _Opt const&&>,
627 is_convertible<_Opt&, _Tp>,
628 is_convertible<_Opt const&, _Tp>,
629 is_convertible<_Opt&&, _Tp>,
630 is_convertible<_Opt const&&, _Tp> >;
631 template <class _Up, class _Opt = optional<_Up>>
632 using __check_assignable_from_opt =
633 _Or< is_assignable<_Tp&, _Opt&>,
634 is_assignable<_Tp&, _Opt const&>,
635 is_assignable<_Tp&, _Opt&&>,
636 is_assignable<_Tp&, _Opt const&&> >;
637 template <class _Up, class _QUp = _QualUp>
638 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
639 return is_convertible<_QUp, _Tp>::value &&
640 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
642 template <class _Up, class _QUp = _QualUp>
643 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
644 return !is_convertible<_QUp, _Tp>::value &&
645 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
647 template <class _Up, class _QUp = _QualUp>
648 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() {
649 // Construction and assignability of _QUp to _Tp has already been
651 return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value;
655 template <class _Up, class _QualUp>
656 using _CheckOptionalLikeCtor =
657 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value,
658 _CheckOptionalLikeConstructor<_QualUp>,
659 __check_tuple_constructor_fail >;
660 template <class _Up, class _QualUp>
661 using _CheckOptionalLikeAssign =
662 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value,
663 _CheckOptionalLikeConstructor<_QualUp>,
664 __check_tuple_constructor_fail >;
667 _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {}
668 _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default;
669 _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&) = default;
670 _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {}
675 class = enable_if_t< _And< _IsSame<_InPlaceT, in_place_t>, is_constructible<value_type, _Args...> >::value > >
676 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args)
677 : __base(in_place, std::forward<_Args>(__args)...) {}
681 class = enable_if_t< is_constructible_v<value_type, initializer_list<_Up>&, _Args...>> >
682 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
683 : __base(in_place, __il, std::forward<_Args>(__args)...) {}
685 template <class _Up = value_type,
686 enable_if_t< _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
687 _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}
689 template <class _Up, enable_if_t< _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>
690 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}
692 // LWG2756: conditionally explicit conversion from const optional<_Up>&
694 enable_if_t< _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
695 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v) {
696 this->__construct_from(__v);
699 enable_if_t< _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
700 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v) {
701 this->__construct_from(__v);
704 // LWG2756: conditionally explicit conversion from optional<_Up>&&
705 template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>
706 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v) {
707 this->__construct_from(std::move(__v));
709 template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>
710 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v) {
711 this->__construct_from(std::move(__v));
714 # if _LIBCPP_STD_VER >= 23
715 template <class _Tag,
718 __enable_if_t<_IsSame<_Tag, __optional_construct_from_invoke_tag>::value, int> = 0>
719 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Tag, _Fp&& __f, _Args&&... __args)
720 : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}
723 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept {
728 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default;
729 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default;
733 class _Up = value_type,
734 class = enable_if_t< _And< _IsNotSame<__remove_cvref_t<_Up>, optional>,
735 _Or< _IsNotSame<__remove_cvref_t<_Up>, value_type>, _Not<is_scalar<value_type>> >,
736 is_constructible<value_type, _Up>,
737 is_assignable<value_type&, _Up> >::value> >
738 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) {
739 if (this->has_value())
740 this->__get() = std::forward<_Up>(__v);
742 this->__construct(std::forward<_Up>(__v));
748 enable_if_t< _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0>
749 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) {
750 this->__assign_from(__v);
755 template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0>
756 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) {
757 this->__assign_from(std::move(__v));
761 template <class... _Args, class = enable_if_t< is_constructible_v<value_type, _Args...> > >
762 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
764 this->__construct(std::forward<_Args>(__args)...);
765 return this->__get();
770 class = enable_if_t< is_constructible_v<value_type, initializer_list<_Up>&, _Args...> > >
771 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
773 this->__construct(__il, std::forward<_Args>(__args)...);
774 return this->__get();
777 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
778 swap(optional& __opt) noexcept(is_nothrow_move_constructible_v<value_type> && is_nothrow_swappable_v<value_type>) {
779 if (this->has_value() == __opt.has_value()) {
781 if (this->has_value())
782 swap(this->__get(), __opt.__get());
784 if (this->has_value()) {
785 __opt.__construct(std::move(this->__get()));
788 this->__construct(std::move(__opt.__get()));
794 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type const> operator->() const noexcept {
795 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
796 return std::addressof(this->__get());
799 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type> operator->() noexcept {
800 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
801 return std::addressof(this->__get());
804 _LIBCPP_HIDE_FROM_ABI constexpr const value_type& operator*() const& noexcept {
805 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
806 return this->__get();
809 _LIBCPP_HIDE_FROM_ABI constexpr value_type& operator*() & noexcept {
810 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
811 return this->__get();
814 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& operator*() && noexcept {
815 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
816 return std::move(this->__get());
819 _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& operator*() const&& noexcept {
820 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
821 return std::move(this->__get());
824 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); }
827 using __base::has_value;
829 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type const& value() const& {
830 if (!this->has_value())
831 __throw_bad_optional_access();
832 return this->__get();
835 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type& value() & {
836 if (!this->has_value())
837 __throw_bad_optional_access();
838 return this->__get();
841 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type&& value() && {
842 if (!this->has_value())
843 __throw_bad_optional_access();
844 return std::move(this->__get());
847 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type const&& value() const&& {
848 if (!this->has_value())
849 __throw_bad_optional_access();
850 return std::move(this->__get());
854 _LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) const& {
855 static_assert(is_copy_constructible_v<value_type>, "optional<T>::value_or: T must be copy constructible");
856 static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
857 return this->has_value() ? this->__get() : static_cast<value_type>(std::forward<_Up>(__v));
861 _LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) && {
862 static_assert(is_move_constructible_v<value_type>, "optional<T>::value_or: T must be move constructible");
863 static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
864 return this->has_value() ? std::move(this->__get()) : static_cast<value_type>(std::forward<_Up>(__v));
867 # if _LIBCPP_STD_VER >= 23
868 template <class _Func>
869 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) & {
870 using _Up = invoke_result_t<_Func, 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");
874 return std::invoke(std::forward<_Func>(__f), value());
875 return remove_cvref_t<_Up>();
878 template <class _Func>
879 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) const& {
880 using _Up = invoke_result_t<_Func, const value_type&>;
881 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
882 "Result of f(value()) must be a specialization of std::optional");
884 return std::invoke(std::forward<_Func>(__f), value());
885 return remove_cvref_t<_Up>();
888 template <class _Func>
889 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) && {
890 using _Up = invoke_result_t<_Func, 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");
894 return std::invoke(std::forward<_Func>(__f), std::move(value()));
895 return remove_cvref_t<_Up>();
898 template <class _Func>
899 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
900 using _Up = invoke_result_t<_Func, const value_type&&>;
901 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
902 "Result of f(std::move(value())) must be a specialization of std::optional");
904 return std::invoke(std::forward<_Func>(__f), std::move(value()));
905 return remove_cvref_t<_Up>();
908 template <class _Func>
909 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) & {
910 using _Up = remove_cv_t<invoke_result_t<_Func, value_type&>>;
911 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
912 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
913 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
914 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
916 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
917 return optional<_Up>();
920 template <class _Func>
921 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) const& {
922 using _Up = remove_cv_t<invoke_result_t<_Func, const value_type&>>;
923 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
924 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
925 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
926 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
928 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
929 return optional<_Up>();
932 template <class _Func>
933 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) && {
934 using _Up = remove_cv_t<invoke_result_t<_Func, value_type&&>>;
935 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
936 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
937 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
938 static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
940 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
941 return optional<_Up>();
944 template <class _Func>
945 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) const&& {
946 using _Up = remove_cvref_t<invoke_result_t<_Func, const value_type&&>>;
947 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
948 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
949 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
950 static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
952 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
953 return optional<_Up>();
956 template <invocable _Func>
957 _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
958 requires is_copy_constructible_v<value_type>
960 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
961 "Result of f() should be the same type as this optional");
964 return std::forward<_Func>(__f)();
967 template <invocable _Func>
968 _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
969 requires is_move_constructible_v<value_type>
971 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
972 "Result of f() should be the same type as this optional");
974 return std::move(*this);
975 return std::forward<_Func>(__f)();
977 # endif // _LIBCPP_STD_VER >= 23
982 # if _LIBCPP_STD_VER >= 17
984 optional(_Tp) -> optional<_Tp>;
987 // Comparisons between optionals
988 template <class _Tp, class _Up>
989 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
990 is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
992 operator==(const optional<_Tp>& __x, const optional<_Up>& __y) {
993 if (static_cast<bool>(__x) != static_cast<bool>(__y))
995 if (!static_cast<bool>(__x))
1000 template <class _Tp, class _Up>
1001 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1002 is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1004 operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) {
1005 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1007 if (!static_cast<bool>(__x))
1009 return *__x != *__y;
1012 template <class _Tp, class _Up>
1013 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1014 is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1016 operator<(const optional<_Tp>& __x, const optional<_Up>& __y) {
1017 if (!static_cast<bool>(__y))
1019 if (!static_cast<bool>(__x))
1024 template <class _Tp, class _Up>
1025 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1026 is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1028 operator>(const optional<_Tp>& __x, const optional<_Up>& __y) {
1029 if (!static_cast<bool>(__x))
1031 if (!static_cast<bool>(__y))
1036 template <class _Tp, class _Up>
1037 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1038 is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1040 operator<=(const optional<_Tp>& __x, const optional<_Up>& __y) {
1041 if (!static_cast<bool>(__x))
1043 if (!static_cast<bool>(__y))
1045 return *__x <= *__y;
1048 template <class _Tp, class _Up>
1049 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1050 is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1052 operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) {
1053 if (!static_cast<bool>(__y))
1055 if (!static_cast<bool>(__x))
1057 return *__x >= *__y;
1060 # if _LIBCPP_STD_VER >= 20
1062 template <class _Tp, three_way_comparable_with<_Tp> _Up>
1063 _LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
1064 operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) {
1066 return *__x <=> *__y;
1067 return __x.has_value() <=> __y.has_value();
1070 # endif // _LIBCPP_STD_VER >= 20
1072 // Comparisons with nullopt
1073 template <class _Tp>
1074 _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept {
1075 return !static_cast<bool>(__x);
1078 # if _LIBCPP_STD_VER <= 17
1080 template <class _Tp>
1081 _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(nullopt_t, const optional<_Tp>& __x) noexcept {
1082 return !static_cast<bool>(__x);
1085 template <class _Tp>
1086 _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, nullopt_t) noexcept {
1087 return static_cast<bool>(__x);
1090 template <class _Tp>
1091 _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(nullopt_t, const optional<_Tp>& __x) noexcept {
1092 return static_cast<bool>(__x);
1095 template <class _Tp>
1096 _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>&, nullopt_t) noexcept {
1100 template <class _Tp>
1101 _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(nullopt_t, const optional<_Tp>& __x) noexcept {
1102 return static_cast<bool>(__x);
1105 template <class _Tp>
1106 _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, nullopt_t) noexcept {
1107 return !static_cast<bool>(__x);
1110 template <class _Tp>
1111 _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(nullopt_t, const optional<_Tp>&) noexcept {
1115 template <class _Tp>
1116 _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, nullopt_t) noexcept {
1117 return static_cast<bool>(__x);
1120 template <class _Tp>
1121 _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(nullopt_t, const optional<_Tp>&) noexcept {
1125 template <class _Tp>
1126 _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>&, nullopt_t) noexcept {
1130 template <class _Tp>
1131 _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(nullopt_t, const optional<_Tp>& __x) noexcept {
1132 return !static_cast<bool>(__x);
1135 # else // _LIBCPP_STD_VER <= 17
1137 template <class _Tp>
1138 _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept {
1139 return __x.has_value() <=> false;
1142 # endif // _LIBCPP_STD_VER <= 17
1144 // Comparisons with T
1145 template <class _Tp, class _Up>
1146 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1147 is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1149 operator==(const optional<_Tp>& __x, const _Up& __v) {
1150 return static_cast<bool>(__x) ? *__x == __v : false;
1153 template <class _Tp, class _Up>
1154 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1155 is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1157 operator==(const _Tp& __v, const optional<_Up>& __x) {
1158 return static_cast<bool>(__x) ? __v == *__x : false;
1161 template <class _Tp, class _Up>
1162 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1163 is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1165 operator!=(const optional<_Tp>& __x, const _Up& __v) {
1166 return static_cast<bool>(__x) ? *__x != __v : true;
1169 template <class _Tp, class _Up>
1170 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1171 is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1173 operator!=(const _Tp& __v, const optional<_Up>& __x) {
1174 return static_cast<bool>(__x) ? __v != *__x : true;
1177 template <class _Tp, class _Up>
1178 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1179 is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1181 operator<(const optional<_Tp>& __x, const _Up& __v) {
1182 return static_cast<bool>(__x) ? *__x < __v : true;
1185 template <class _Tp, class _Up>
1186 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1187 is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1189 operator<(const _Tp& __v, const optional<_Up>& __x) {
1190 return static_cast<bool>(__x) ? __v < *__x : false;
1193 template <class _Tp, class _Up>
1194 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1195 is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1197 operator<=(const optional<_Tp>& __x, const _Up& __v) {
1198 return static_cast<bool>(__x) ? *__x <= __v : true;
1201 template <class _Tp, class _Up>
1202 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1203 is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1205 operator<=(const _Tp& __v, const optional<_Up>& __x) {
1206 return static_cast<bool>(__x) ? __v <= *__x : false;
1209 template <class _Tp, class _Up>
1210 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1211 is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1213 operator>(const optional<_Tp>& __x, const _Up& __v) {
1214 return static_cast<bool>(__x) ? *__x > __v : false;
1217 template <class _Tp, class _Up>
1218 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1219 is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1221 operator>(const _Tp& __v, const optional<_Up>& __x) {
1222 return static_cast<bool>(__x) ? __v > *__x : true;
1225 template <class _Tp, class _Up>
1226 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1227 is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1229 operator>=(const optional<_Tp>& __x, const _Up& __v) {
1230 return static_cast<bool>(__x) ? *__x >= __v : false;
1233 template <class _Tp, class _Up>
1234 _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1235 is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1237 operator>=(const _Tp& __v, const optional<_Up>& __x) {
1238 return static_cast<bool>(__x) ? __v >= *__x : true;
1241 # if _LIBCPP_STD_VER >= 20
1243 template <class _Tp, class _Up>
1244 requires(!__is_derived_from_optional<_Up>) && three_way_comparable_with<_Tp, _Up>
1245 _LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
1246 operator<=>(const optional<_Tp>& __x, const _Up& __v) {
1247 return __x.has_value() ? *__x <=> __v : strong_ordering::less;
1250 # endif // _LIBCPP_STD_VER >= 20
1252 template <class _Tp>
1253 inline _LIBCPP_HIDE_FROM_ABI
1254 _LIBCPP_CONSTEXPR_SINCE_CXX20 enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, void >
1255 swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
1259 template <class _Tp>
1260 _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
1261 return optional<decay_t<_Tp>>(std::forward<_Tp>(__v));
1264 template <class _Tp, class... _Args>
1265 _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
1266 return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
1269 template <class _Tp, class _Up, class... _Args>
1270 _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) {
1271 return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
1274 template <class _Tp>
1275 struct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > {
1276 # if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1277 _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type;
1278 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
1281 _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
1282 return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
1286 _LIBCPP_END_NAMESPACE_STD
1288 #endif // _LIBCPP_STD_VER >= 17
1292 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1295 # include <concepts>
1297 # include <iterator>
1301 # include <stdexcept>
1303 # include <type_traits>
1304 # include <typeinfo>
1309 #endif // _LIBCPP_OPTIONAL