1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_OPTIONAL
11#define _LIBCPP_OPTIONAL
12
13/*
14 optional synopsis
15
16// C++1z
17
18namespace std {
19 // [optional.optional], class template optional
20 template <class T>
21 class optional;
22
23 template<class T>
24 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 only
30 []<class U>(const optional<U>&){ }(t);
31 };
32
33 // [optional.nullopt], no-value state indicator
34 struct nullopt_t{see below };
35 inline constexpr nullopt_t nullopt(unspecified );
36
37 // [optional.bad.access], class bad_optional_access
38 class bad_optional_access;
39
40 // [optional.relops], relational operators
41 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++20
56
57 // [optional.nullops], comparison with nullopt
58 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++17
60 template<class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; // until C++17
61 template<class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; // until C++17
62 template<class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept; // until C++17
63 template<class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept; // until C++17
64 template<class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; // until C++17
65 template<class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; // until C++17
66 template<class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept; // until C++17
67 template<class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept; // until C++17
68 template<class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; // until C++17
69 template<class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; // until C++17
70 template<class T>
71 constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // since C++20
72
73 // [optional.comp.with.t], comparison with T
74 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++20
90
91 // [optional.specalg], specialized algorithms
92 template<class T>
93 void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20
94
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 support
103 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], constructors
114 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++20
126 template<class U>
127 explicit(see-below) optional(optional<U> &&); // constexpr in C++20
128
129 // [optional.dtor], destructor
130 ~optional(); // constexpr in C++20
131
132 // [optional.assign], assignment
133 optional &operator=(nullopt_t) noexcept; // constexpr in C++20
134 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++20
137 template<class U> optional &operator=(const optional<U> &); // constexpr in C++20
138 template<class U> optional &operator=(optional<U> &&); // constexpr in C++20
139 template<class... Args> T& emplace(Args &&...); // constexpr in C++20
140 template<class U, class... Args> T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20
141
142 // [optional.swap], swap
143 void swap(optional &) noexcept(see below ); // constexpr in C++20
144
145 // [optional.iterators], iterator support
146 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], observers
152 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 operations
168 template<class F> constexpr auto and_then(F&& f) &; // since C++23
169 template<class F> constexpr auto and_then(F&& f) &&; // since C++23
170 template<class F> constexpr auto and_then(F&& f) const&; // since C++23
171 template<class F> constexpr auto and_then(F&& f) const&&; // since C++23
172 template<class F> constexpr auto transform(F&& f) &; // since C++23
173 template<class F> constexpr auto transform(F&& f) &&; // since C++23
174 template<class F> constexpr auto transform(F&& f) const&; // since C++23
175 template<class F> constexpr auto transform(F&& f) const&&; // since C++23
176 template<class F> constexpr optional or_else(F&& f) &&; // since C++23
177 template<class F> constexpr optional or_else(F&& f) const&; // since C++23
178
179 // [optional.mod], modifiers
180 void reset() noexcept; // constexpr in C++20
181
182 private:
183 T *val; // exposition only
184 };
185
186 template<class T>
187 optional(T) -> optional<T>;
188
189 template<class T>
190 class optional<T&> { // since C++26
191 public:
192 using value_type = T;
193 using iterator = implementation-defined; // see [optional.ref.iterators]
194
195 public:
196 // [optional.ref.ctor], constructors
197 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], assignment
217 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], swap
223 constexpr void swap(optional& rhs) noexcept;
224
225 // [optional.ref.iterators], iterator support
226 constexpr iterator begin() const noexcept;
227 constexpr iterator end() const noexcept;
228
229 // [optional.ref.observe], observers
230 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-deleted
235 template<class U = remove_cv_t<T>>
236 constexpr remove_cv_t<T> value_or(U&& u) const;
237
238 // [optional.ref.monadic], monadic operations
239 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], modifiers
244 constexpr void reset() noexcept;
245
246 private:
247 T* val = nullptr; // exposition only
248
249 // [optional.ref.expos], exposition only helper functions
250 template<class U>
251 constexpr void convert-ref-init-val(U&& u); // exposition only
252 };
253
254} // namespace std
255
256*/
257
258#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
259# include <__cxx03/__config>
260#else
261# 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 <__exception/exception.h>
268# include <__functional/hash.h>
269# include <__functional/invoke.h>
270# include <__functional/unary_function.h>
271# include <__fwd/format.h>
272# include <__fwd/functional.h>
273# include <__iterator/bounded_iter.h>
274# include <__iterator/capacity_aware_iterator.h>
275# include <__memory/addressof.h>
276# include <__memory/construct_at.h>
277# include <__ranges/enable_borrowed_range.h>
278# include <__ranges/enable_view.h>
279# include <__tuple/sfinae_helpers.h>
280# include <__type_traits/add_pointer.h>
281# include <__type_traits/conditional.h>
282# include <__type_traits/conjunction.h>
283# include <__type_traits/decay.h>
284# include <__type_traits/disjunction.h>
285# include <__type_traits/enable_if.h>
286# include <__type_traits/integral_constant.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 includes
322
323// [optional.syn]
324# include <compare>
325
326# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
327# pragma GCC system_header
328# endif
329
330_LIBCPP_PUSH_MACROS
331# include <__undef_macros>
332
333namespace std // purposefully not using versioning namespace
334{
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 dylib
342 ~bad_optional_access() _NOEXCEPT override;
343 [[__nodiscard__]] const char* what() const _NOEXCEPT override;
344};
345
346} // namespace std
347
348# if _LIBCPP_STD_VER >= 17
349
350_LIBCPP_BEGIN_NAMESPACE_STD
351
352[[noreturn]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_optional_access() {
353# if _LIBCPP_HAS_EXCEPTIONS
354 throw bad_optional_access();
355# else
356 _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode");
357# endif
358}
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 >= 23
396 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# endif
401
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 >= 23
427 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# endif
432
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 [[nodiscard]] _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 else
475 __construct(std::forward<_That>(__opt).__get());
476 }
477 }
478
479 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
480 __swap(__optional_storage_base& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>) {
481 using std::swap;
482 if (this->has_value() == __rhs.has_value()) {
483 if (this->has_value())
484 swap(this->__get(), __rhs.__get());
485 } else {
486 if (this->has_value()) {
487 __rhs.__construct(std::move(this->__get()));
488 this->reset();
489 } else {
490 this->__construct(std::move(__rhs.__get()));
491 __rhs.reset();
492 }
493 }
494 }
495
496 // [optional.observe]
497 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp const> operator->() const noexcept {
498 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
499 return std::addressof(this->__get());
500 }
501
502 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() noexcept {
503 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
504 return std::addressof(this->__get());
505 }
506
507 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
508 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
509 return this->__get();
510 }
511
512 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
513 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
514 return this->__get();
515 }
516
517 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
518 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
519 return std::move(this->__get());
520 }
521
522 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
523 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
524 return std::move(this->__get());
525 }
526
527 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& {
528 if (!this->has_value())
529 std::__throw_bad_optional_access();
530 return this->__get();
531 }
532
533 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
534 if (!this->has_value())
535 std::__throw_bad_optional_access();
536 return this->__get();
537 }
538
539 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
540 if (!this->has_value())
541 std::__throw_bad_optional_access();
542 return std::move(this->__get());
543 }
544
545 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& {
546 if (!this->has_value())
547 std::__throw_bad_optional_access();
548 return std::move(this->__get());
549 }
550};
551
552template <class _Tp>
553struct __optional_storage_base<_Tp, true> {
554 using value_type = _Tp;
555 using __raw_type _LIBCPP_NODEBUG = remove_reference_t<_Tp>;
556 __raw_type* __value_;
557
558 _LIBCPP_HIDE_FROM_ABI constexpr __optional_storage_base() noexcept : __value_(nullptr) {}
559
560 template <class _Up>
561 _LIBCPP_HIDE_FROM_ABI constexpr void __convert_init_ref_val(_Up&& __val) {
562 _Tp& __r(std::forward<_Up>(__val));
563 __value_ = std::addressof(__r);
564 }
565
566 template <class _UArg>
567 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg) {
568 static_assert(!__reference_constructs_from_temporary_v<_Tp, _UArg>,
569 "Attempted to construct a reference element in optional from a "
570 "possible temporary");
571 __convert_init_ref_val(std::forward<_UArg>(__uarg));
572 }
573
574# if _LIBCPP_STD_VER >= 23
575 template <class _Fp, class... _Args>
576 constexpr __optional_storage_base(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) {
577 __convert_init_ref_val(std::forward<invoke_result_t<_Fp, _Args...>>(
578 std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)));
579 }
580# endif
581
582 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; }
583
584 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; }
585
586 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const noexcept { return *__value_; }
587
588 template <class _UArg>
589 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_UArg&& __val) {
590 static_assert(!__reference_constructs_from_temporary_v<_Tp, _UArg>,
591 "Attempted to construct a reference element in tuple from a "
592 "possible temporary");
593 __convert_init_ref_val(std::forward<_UArg>(__val));
594 }
595
596 template <class _That>
597 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
598 if (__opt.has_value())
599 __construct(std::forward<_That>(__opt).__get());
600 }
601
602 template <class _That>
603 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
604 if (has_value() == __opt.has_value()) {
605 if (has_value())
606 *__value_ = std::forward<_That>(__opt).__get();
607 } else {
608 if (has_value())
609 reset();
610 else
611 __construct(std::forward<_That>(__opt).__get());
612 }
613 }
614
615 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__optional_storage_base& __rhs) noexcept {
616 std::swap(__value_, __rhs.__value_);
617 }
618
619 // [optional.ref.observe]
620 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() const noexcept {
621 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
622 return std::addressof(this->__get());
623 }
624
625 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() const noexcept {
626 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
627 return this->__get();
628 }
629
630 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() const {
631 if (!this->has_value())
632 std::__throw_bad_optional_access();
633 return this->__get();
634 }
635};
636
637template <class _Tp, bool = is_trivially_copy_constructible_v<_Tp>>
638struct __optional_copy_base : __optional_storage_base<_Tp> {
639 using __optional_storage_base<_Tp>::__optional_storage_base;
640};
641
642template <class _Tp>
643struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> {
644 using __optional_storage_base<_Tp>::__optional_storage_base;
645
646 _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default;
647
648 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) {
649 this->__construct_from(__opt);
650 }
651
652 _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&) = default;
653 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default;
654 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&) = default;
655};
656
657template <class _Tp, bool = is_trivially_move_constructible_v<_Tp>>
658struct __optional_move_base : __optional_copy_base<_Tp> {
659 using __optional_copy_base<_Tp>::__optional_copy_base;
660};
661
662template <class _Tp>
663struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> {
664 using value_type = _Tp;
665 using __optional_copy_base<_Tp>::__optional_copy_base;
666
667 _LIBCPP_HIDE_FROM_ABI __optional_move_base() = default;
668 _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default;
669
670 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
671 __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) {
672 this->__construct_from(std::move(__opt));
673 }
674
675 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default;
676 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&) = default;
677};
678
679template <class _Tp,
680 bool = (is_trivially_destructible_v<_Tp> && is_trivially_copy_constructible_v<_Tp> &&
681 is_trivially_copy_assignable_v<_Tp>)>
682struct __optional_copy_assign_base : __optional_move_base<_Tp> {
683 using __optional_move_base<_Tp>::__optional_move_base;
684};
685
686template <class _Tp>
687struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> {
688 using __optional_move_base<_Tp>::__optional_move_base;
689
690 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base() = default;
691 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
692 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&) = default;
693
694 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base&
695 operator=(const __optional_copy_assign_base& __opt) {
696 this->__assign_from(__opt);
697 return *this;
698 }
699
700 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
701};
702
703template <class _Tp,
704 bool = (is_trivially_destructible_v<_Tp> && is_trivially_move_constructible_v<_Tp> &&
705 is_trivially_move_assignable_v<_Tp>)>
706struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> {
707 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
708};
709
710template <class _Tp>
711struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> {
712 using value_type = _Tp;
713 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
714
715 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base() = default;
716 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;
717 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&) = default;
718 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
719
720 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base&
721 operator=(__optional_move_assign_base&& __opt) noexcept(
722 is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) {
723 this->__assign_from(std::move(__opt));
724 return *this;
725 }
726};
727
728template <class _Tp>
729using __optional_sfinae_ctor_base_t _LIBCPP_NODEBUG =
730 __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >;
731
732template <class _Tp>
733using __optional_sfinae_assign_base_t _LIBCPP_NODEBUG =
734 __sfinae_assign_base< (is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Tp>),
735 (is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp>)>;
736
737template <class _Tp>
738class optional;
739
740# if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
741template <class _Tp>
742constexpr bool ranges::enable_view<optional<_Tp>> = true;
743
744template <class _Tp>
745constexpr range_format format_kind<optional<_Tp>> = range_format::disabled;
746
747template <class _Tp>
748constexpr bool ranges::enable_borrowed_range<optional<_Tp&>> = true;
749
750# endif
751
752# if _LIBCPP_STD_VER >= 20
753
754template <class _Tp>
755concept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(const optional<_Up>&) {}(__t); };
756
757# endif // _LIBCPP_STD_VER >= 20
758
759template <class _Tp>
760struct __is_std_optional : false_type {};
761template <class _Tp>
762struct __is_std_optional<optional<_Tp>> : true_type {};
763
764# if _LIBCPP_STD_VER < 26
765template <class _Tp>
766inline constexpr bool __is_valid_optional_contained_type = is_object_v<_Tp>;
767# else
768template <class _Tp>
769inline constexpr bool __is_valid_optional_contained_type = is_object_v<_Tp> || is_lvalue_reference_v<_Tp>;
770# endif
771
772template <class _Tp>
773struct __optional_iterator_base : __optional_move_assign_base<_Tp> {
774 using __optional_move_assign_base<_Tp>::__optional_move_assign_base;
775};
776
777# if _LIBCPP_STD_VER >= 26
778template <class _Tp>
779struct __optional_iterator_base<_Tp&> : __optional_storage_base<_Tp&> {
780 using __optional_storage_base<_Tp&>::__optional_storage_base;
781};
782
783# if _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
784
785template <class _Tp>
786 requires is_object_v<_Tp>
787struct __optional_iterator_base<_Tp> : __optional_move_assign_base<_Tp> {
788private:
789 using __pointer _LIBCPP_NODEBUG = add_pointer_t<_Tp>;
790 using __const_pointer _LIBCPP_NODEBUG = add_pointer_t<const _Tp>;
791
792public:
793 using __optional_move_assign_base<_Tp>::__optional_move_assign_base;
794
795# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
796 using iterator = __bounded_iter<__pointer>;
797 using const_iterator = __bounded_iter<__const_pointer>;
798# else
799 using iterator = __capacity_aware_iterator<__pointer, optional<_Tp>, 1>;
800 using const_iterator = __capacity_aware_iterator<__const_pointer, optional<_Tp>, 1>;
801# endif
802
803 // [optional.iterators], iterator support
804 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {
805 auto* __ptr = std::addressof(this->__get());
806
807# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
808 return std::__make_bounded_iter(__ptr, __ptr, __ptr + (this->has_value() ? 1 : 0));
809# else
810 return std::__make_capacity_aware_iterator<__pointer, optional<_Tp>, 1>(__ptr);
811# endif
812 }
813
814 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {
815 auto* __ptr = std::addressof(this->__get());
816
817# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
818 return std::__make_bounded_iter(__ptr, __ptr, __ptr + (this->has_value() ? 1 : 0));
819# else
820 return std::__make_capacity_aware_iterator<__const_pointer, optional<_Tp>, 1>(__ptr);
821# endif
822 }
823
824 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept {
825 return begin() + (this->has_value() ? 1 : 0);
826 }
827 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept {
828 return begin() + (this->has_value() ? 1 : 0);
829 }
830};
831
832template <class _Tp>
833 requires(is_object_v<_Tp> && !__is_unbounded_array_v<_Tp>)
834struct __optional_iterator_base<_Tp&> : __optional_storage_base<_Tp&> {
835private:
836 using __pointer _LIBCPP_NODEBUG = add_pointer_t<_Tp>;
837
838public:
839 using __optional_storage_base<_Tp&>::__optional_storage_base;
840
841# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
842 using iterator = __bounded_iter<__pointer>;
843# else
844 using iterator = __capacity_aware_iterator<__pointer, optional<_Tp&>, 1>;
845# endif
846
847 // [optional.ref.iterators], iterator support
848
849 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const noexcept {
850 auto* __ptr = this->has_value() ? std::addressof(this->__get()) : nullptr;
851
852# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
853 return std::__make_bounded_iter(__ptr, __ptr, __ptr + (this->has_value() ? 1 : 0));
854# else
855 return std::__make_capacity_aware_iterator<__pointer, optional<_Tp&>, 1>(__ptr);
856# endif
857 }
858
859 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const noexcept {
860 return begin() + (this->has_value() ? 1 : 0);
861 }
862};
863
864# endif // _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
865# endif // _LIBCPP_STD_VER >= 26
866
867template <class _Tp>
868class _LIBCPP_DECLSPEC_EMPTY_BASES optional
869 : public __optional_iterator_base<_Tp>,
870 private __optional_sfinae_ctor_base_t<_Tp>,
871 private __optional_sfinae_assign_base_t<_Tp> {
872 using __base _LIBCPP_NODEBUG = __optional_iterator_base<_Tp>;
873
874public:
875 using value_type = __libcpp_remove_reference_t<_Tp>;
876
877 using __trivially_relocatable _LIBCPP_NODEBUG =
878 conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, optional, void>;
879
880private:
881 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>, "instantiation of optional with in_place_t is ill-formed");
882 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed");
883# if _LIBCPP_STD_VER >= 26
884 static_assert(!is_rvalue_reference_v<_Tp>, "instantiation of optional with an rvalue reference type is ill-formed");
885# else
886 static_assert(!is_reference_v<_Tp>, "instantiation of optional with a reference type is ill-formed");
887# endif
888 static_assert(is_destructible_v<_Tp>, "instantiation of optional with a non-destructible type is ill-formed");
889 static_assert(!is_array_v<_Tp>, "instantiation of optional with an array type is ill-formed");
890
891 // LWG2756: conditionally explicit conversion from _Up
892 struct _CheckOptionalArgsConstructor {
893 template <class _Up>
894 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
895 return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>;
896 }
897
898 template <class _Up>
899 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
900 return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>;
901 }
902 };
903 template <class _Up>
904 using _CheckOptionalArgsCtor _LIBCPP_NODEBUG =
905 _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value &&
906 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value),
907 _CheckOptionalArgsConstructor,
908 __check_tuple_constructor_fail >;
909 template <class _QualUp>
910 struct _CheckOptionalLikeConstructor {
911 template <class _Up, class _Opt = optional<_Up>>
912 using __check_constructible_from_opt _LIBCPP_NODEBUG =
913 _Or< is_constructible<_Tp, _Opt&>,
914 is_constructible<_Tp, _Opt const&>,
915 is_constructible<_Tp, _Opt&&>,
916 is_constructible<_Tp, _Opt const&&>,
917 is_convertible<_Opt&, _Tp>,
918 is_convertible<_Opt const&, _Tp>,
919 is_convertible<_Opt&&, _Tp>,
920 is_convertible<_Opt const&&, _Tp> >;
921 template <class _Up, class _Opt = optional<_Up>>
922 using __check_assignable_from_opt _LIBCPP_NODEBUG =
923 _Or< is_assignable<_Tp&, _Opt&>,
924 is_assignable<_Tp&, _Opt const&>,
925 is_assignable<_Tp&, _Opt&&>,
926 is_assignable<_Tp&, _Opt const&&> >;
927 template <class _Up, class _QUp = _QualUp>
928 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
929 return is_convertible<_QUp, _Tp>::value &&
930 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
931 }
932 template <class _Up, class _QUp = _QualUp>
933 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
934 return !is_convertible<_QUp, _Tp>::value &&
935 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
936 }
937 template <class _Up, class _QUp = _QualUp>
938 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() {
939 // Construction and assignability of _QUp to _Tp has already been
940 // checked.
941 return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value;
942 }
943 };
944
945 template <class _Up, class _QualUp>
946 using _CheckOptionalLikeCtor _LIBCPP_NODEBUG =
947 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value,
948 _CheckOptionalLikeConstructor<_QualUp>,
949 __check_tuple_constructor_fail >;
950 template <class _Up, class _QualUp>
951 using _CheckOptionalLikeAssign _LIBCPP_NODEBUG =
952 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value,
953 _CheckOptionalLikeConstructor<_QualUp>,
954 __check_tuple_constructor_fail >;
955
956public:
957 _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {}
958 _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default;
959 _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&) = default;
960 _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {}
961
962 template <class _InPlaceT,
963 class... _Args,
964 enable_if_t<_And<_IsSame<_InPlaceT, in_place_t>, is_constructible<_Tp, _Args...>>::value, int> = 0>
965 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args)
966 : __base(in_place, std::forward<_Args>(__args)...) {}
967
968 template <class _Up, class... _Args, enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
969 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
970 : __base(in_place, __il, std::forward<_Args>(__args)...) {}
971
972 template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
973 _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) noexcept(is_nothrow_constructible_v<_Tp, _Up>) // strengthened
974 : __base(in_place, std::forward<_Up>(__v)) {}
975
976 template <class _Up = remove_cv_t<_Tp>,
977 enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>
978 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v) noexcept(
979 is_nothrow_constructible_v<_Tp, _Up>) // strengthened
980 : __base(in_place, std::forward<_Up>(__v)) {}
981
982 // LWG2756: conditionally explicit conversion from const optional<_Up>&
983 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
984 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v) {
985 this->__construct_from(__v);
986 }
987
988 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
989 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v) {
990 this->__construct_from(__v);
991 }
992
993 // LWG2756: conditionally explicit conversion from optional<_Up>&&
994 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>
995 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v) {
996 this->__construct_from(std::move(__v));
997 }
998
999 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>
1000 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v) {
1001 this->__construct_from(std::move(__v));
1002 }
1003
1004# if _LIBCPP_STD_VER >= 23
1005 template <class _Tag,
1006 class _Fp,
1007 class... _Args,
1008 enable_if_t<_IsSame<_Tag, __optional_construct_from_invoke_tag>::value, int> = 0>
1009 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Tag, _Fp&& __f, _Args&&... __args)
1010 : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}
1011# endif
1012
1013 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept {
1014 reset();
1015 return *this;
1016 }
1017
1018 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default;
1019 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default;
1020
1021 // LWG2756
1022 template <class _Up = remove_cv_t<_Tp>,
1023 enable_if_t<_And<_IsNotSame<__remove_cvref_t<_Up>, optional>,
1024 _Or<_IsNotSame<__remove_cvref_t<_Up>, _Tp>, _Not<is_scalar<_Tp>>>,
1025 is_constructible<_Tp, _Up>,
1026 is_assignable<_Tp&, _Up>>::value,
1027 int> = 0>
1028 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) {
1029 if (this->has_value())
1030 this->__get() = std::forward<_Up>(__v);
1031 else
1032 this->__construct(std::forward<_Up>(__v));
1033 return *this;
1034 }
1035
1036 // LWG2756
1037 template <class _Up, enable_if_t<_CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0>
1038 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) {
1039 this->__assign_from(__v);
1040 return *this;
1041 }
1042
1043 // LWG2756
1044 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0>
1045 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) {
1046 this->__assign_from(std::move(__v));
1047 return *this;
1048 }
1049
1050 template <class... _Args, enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1051 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1052 reset();
1053 this->__construct(std::forward<_Args>(__args)...);
1054 return this->__get();
1055 }
1056
1057 template <class _Up, class... _Args, enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1058 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1059 reset();
1060 this->__construct(__il, std::forward<_Args>(__args)...);
1061 return this->__get();
1062 }
1063
1064 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
1065 swap(optional& __opt) noexcept((is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>)) {
1066 this->__swap(__opt);
1067 }
1068
1069 using __base::operator*;
1070 using __base::operator->;
1071
1072 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); }
1073
1074 using __base::__get;
1075 using __base::has_value;
1076 using __base::value;
1077
1078 template <class _Up = remove_cv_t<_Tp>>
1079 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
1080 static_assert(is_copy_constructible_v<_Tp>, "optional<T>::value_or: T must be copy constructible");
1081 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
1082 return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));
1083 }
1084
1085 template <class _Up = remove_cv_t<_Tp>>
1086 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
1087 static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
1088 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
1089 return this->has_value() ? std::move(this->__get()) : static_cast<_Tp>(std::forward<_Up>(__v));
1090 }
1091
1092# if _LIBCPP_STD_VER >= 23
1093 template <class _Func>
1094 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
1095 using _Up = invoke_result_t<_Func, _Tp&>;
1096 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1097 "Result of f(value()) must be a specialization of std::optional");
1098 if (*this)
1099 return std::invoke(std::forward<_Func>(__f), value());
1100 return remove_cvref_t<_Up>();
1101 }
1102
1103 template <class _Func>
1104 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
1105 using _Up = invoke_result_t<_Func, const _Tp&>;
1106 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1107 "Result of f(value()) must be a specialization of std::optional");
1108 if (*this)
1109 return std::invoke(std::forward<_Func>(__f), value());
1110 return remove_cvref_t<_Up>();
1111 }
1112
1113 template <class _Func>
1114 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
1115 using _Up = invoke_result_t<_Func, _Tp&&>;
1116 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1117 "Result of f(std::move(value())) must be a specialization of std::optional");
1118 if (*this)
1119 return std::invoke(std::forward<_Func>(__f), std::move(value()));
1120 return remove_cvref_t<_Up>();
1121 }
1122
1123 template <class _Func>
1124 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
1125 using _Up = invoke_result_t<_Func, const _Tp&&>;
1126 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1127 "Result of f(std::move(value())) must be a specialization of std::optional");
1128 if (*this)
1129 return std::invoke(std::forward<_Func>(__f), std::move(value()));
1130 return remove_cvref_t<_Up>();
1131 }
1132
1133 template <class _Func>
1134 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1135 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
1136 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
1137 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
1138 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
1139 static_assert(
1140 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
1141 if (*this)
1142 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
1143 return optional<_Up>();
1144 }
1145
1146 template <class _Func>
1147 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1148 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;
1149 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
1150 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
1151 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
1152 static_assert(
1153 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
1154 if (*this)
1155 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
1156 return optional<_Up>();
1157 }
1158
1159 template <class _Func>
1160 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1161 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;
1162 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
1163 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
1164 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
1165 static_assert(
1166 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
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 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1174 using _Up = remove_cv_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(
1179 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
1180 if (*this)
1181 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
1182 return optional<_Up>();
1183 }
1184
1185 template <invocable _Func>
1186 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
1187 requires is_copy_constructible_v<_Tp>
1188 {
1189 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
1190 "Result of f() should be the same type as this optional");
1191 if (*this)
1192 return *this;
1193 return std::forward<_Func>(__f)();
1194 }
1195
1196 template <invocable _Func>
1197 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
1198 requires is_move_constructible_v<_Tp>
1199 {
1200 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
1201 "Result of f() should be the same type as this optional");
1202 if (*this)
1203 return std::move(*this);
1204 return std::forward<_Func>(__f)();
1205 }
1206# endif // _LIBCPP_STD_VER >= 23
1207
1208 using __base::reset;
1209};
1210
1211# if _LIBCPP_STD_VER >= 26
1212template <class _Tp>
1213class optional<_Tp&> : public __optional_iterator_base<_Tp&> {
1214 using __base _LIBCPP_NODEBUG = __optional_iterator_base<_Tp&>;
1215
1216 template <class _Up, class _QualUp>
1217 static constexpr bool __check_optionalU_ctor =
1218 !is_same_v<remove_cv_t<_Tp>, optional<_Up>> && !is_same_v<_Tp&, _Up> && is_constructible_v<_Tp&, _QualUp>;
1219 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>, "instantiation of optional with in_place_t is ill-formed");
1220 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed");
1221
1222public:
1223 using value_type = _Tp;
1224
1225 constexpr optional() noexcept = default;
1226 constexpr optional(nullopt_t) noexcept {}
1227 constexpr optional(const optional&) noexcept = default;
1228
1229 template <class _Arg>
1230 requires(is_constructible_v<_Tp&, _Arg> && !reference_constructs_from_temporary_v<_Tp&, _Arg>)
1231 constexpr explicit optional(in_place_t, _Arg&& __arg) : __base(in_place, std::forward<_Arg>(__arg)) {}
1232
1233 template <class _Up>
1234 requires(!is_same_v<remove_cvref_t<_Up>, optional> && !is_same_v<remove_cvref_t<_Up>, in_place_t> &&
1235 is_constructible_v<_Tp&, _Up> && !reference_constructs_from_temporary_v<_Tp&, _Up>)
1236 constexpr explicit(!is_convertible_v<_Up, _Tp&>) optional(_Up&& __v) noexcept(is_nothrow_constructible_v<_Tp&, _Up>)
1237 : __base(in_place, std::forward<_Up>(__v)) {}
1238
1239 template <class _Up>
1240 requires(__check_optionalU_ctor<_Up, _Up&> && !reference_constructs_from_temporary_v<_Tp&, _Up&>)
1241 constexpr explicit(!is_convertible_v<_Up&, _Tp&>)
1242 optional(optional<_Up>& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, _Up&>) {
1243 this->__construct_from(__rhs);
1244 }
1245
1246 template <class _Up>
1247 requires(__check_optionalU_ctor<_Up, const _Up&> && !reference_constructs_from_temporary_v<_Tp&, const _Up&>)
1248 constexpr explicit(!is_convertible_v<const _Up&, _Tp&>)
1249 optional(const optional<_Up>& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, const _Up&>) {
1250 this->__construct_from(__rhs);
1251 }
1252
1253 template <class _Up>
1254 requires(__check_optionalU_ctor<_Up, _Up> && !reference_constructs_from_temporary_v<_Tp&, _Up>)
1255 constexpr explicit(!is_convertible_v<_Up, _Tp&>)
1256 optional(optional<_Up>&& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, _Up>) {
1257 this->__construct_from(std::move(__rhs));
1258 }
1259
1260 template <class _Up>
1261 requires(__check_optionalU_ctor<_Up, const _Up> && !reference_constructs_from_temporary_v<_Tp&, const _Up>)
1262 constexpr explicit(!is_convertible_v<const _Up, _Tp&>)
1263 optional(const optional<_Up>&& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, const _Up>) {
1264 this->__construct_from(std::move(__rhs));
1265 }
1266
1267 template <class _Tag, class _Fp, class... _Args>
1268 requires(is_same_v<_Tag, __optional_construct_from_invoke_tag>)
1269 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Tag, _Fp&& __f, _Args&&... __args)
1270 : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}
1271
1272 // deleted overloads
1273
1274 template <class _Up>
1275 requires(!is_same_v<remove_cvref_t<_Up>, optional> && !is_same_v<remove_cvref_t<_Up>, in_place_t> &&
1276 is_constructible_v<_Tp&, _Up> && reference_constructs_from_temporary_v<_Tp&, _Up>)
1277 constexpr explicit(!is_convertible_v<_Up, _Tp&>)
1278 optional(_Up&& __v) noexcept(is_nothrow_constructible_v<_Tp&, _Up>) = delete;
1279
1280 template <class _Up>
1281 requires(__check_optionalU_ctor<_Up, _Up&> && reference_constructs_from_temporary_v<_Tp&, _Up&>)
1282 constexpr explicit(!is_convertible_v<_Up&, _Tp&>)
1283 optional(optional<_Up>& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, _Up&>) = delete;
1284
1285 template <class _Up>
1286 requires(__check_optionalU_ctor<_Up, const _Up&> && reference_constructs_from_temporary_v<_Tp&, const _Up&>)
1287 constexpr explicit(!is_convertible_v<const _Up&, _Tp&>)
1288 optional(const optional<_Up>& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, const _Up&>) = delete;
1289
1290 template <class _Up>
1291 requires(__check_optionalU_ctor<_Up, _Up> && reference_constructs_from_temporary_v<_Tp&, _Up>)
1292 constexpr explicit(!is_convertible_v<_Up, _Tp&>)
1293 optional(optional<_Up>&& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, _Up>) = delete;
1294
1295 template <class _Up>
1296 requires(__check_optionalU_ctor<_Up, const _Up> && reference_constructs_from_temporary_v<_Tp&, const _Up>)
1297 constexpr explicit(!is_convertible_v<const _Up, _Tp&>)
1298 optional(const optional<_Up>&& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, const _Up>) = delete;
1299
1300 constexpr ~optional() = default;
1301
1302 using __base::__get;
1303
1304 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(nullopt_t) noexcept {
1305 reset();
1306 return *this;
1307 }
1308
1309 constexpr optional& operator=(const optional&) noexcept = default;
1310
1311 template <class _Up>
1312 requires(is_constructible_v<_Tp&, _Up> && !reference_constructs_from_temporary_v<_Tp&, _Up>)
1313 constexpr _Tp& emplace(_Up&& __u) noexcept(is_nothrow_constructible_v<_Tp&, _Up>) {
1314 this->__construct(std::forward<_Up>(__u));
1315
1316 return this->__get();
1317 }
1318
1319 constexpr void swap(optional& __rhs) noexcept { this->__swap(__rhs); }
1320
1321 using __base::operator->;
1322 using __base::operator*;
1323
1324 constexpr explicit operator bool() const noexcept { return has_value(); }
1325
1326 using __base::has_value;
1327 using __base::value;
1328
1329 template <class _Up = remove_cv_t<_Tp>>
1330 requires(!is_array_v<_Tp> && is_object_v<_Tp>)
1331 [[nodiscard]] constexpr decay_t<_Tp> value_or(_Up&& __v) const {
1332 using _XTp = remove_cv_t<_Tp>;
1333 static_assert(is_constructible_v<_XTp, _Tp&>, "optional<T&>::value_or: remove_cv_t<T> must be constructible");
1334 static_assert(is_convertible_v<_Up, _XTp>, "optional<T&>::value_or: U must be convertible to remove_cv_t<T>");
1335 return this->has_value() ? this->__get() : static_cast<_XTp>(std::forward<_Up>(__v));
1336 }
1337
1338 template <class _Func>
1339 [[nodiscard]] constexpr auto and_then(_Func&& __f) const {
1340 using _Up = invoke_result_t<_Func, _Tp&>;
1341 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1342 "Result of f(value()) must be a specialization of std::optional");
1343 if (*this)
1344 return std::invoke(std::forward<_Func>(__f), value());
1345 return remove_cvref_t<_Up>();
1346 }
1347
1348 template <class _Func>
1349 [[nodiscard]] constexpr optional<remove_cv_t<invoke_result_t<_Func, _Tp&>>> transform(_Func&& __f) const {
1350 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
1351 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
1352 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
1353 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
1354 static_assert(
1355 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
1356
1357 if (*this)
1358 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
1359 return optional<_Up>();
1360 }
1361
1362 template <invocable _Func>
1363 [[nodiscard]] constexpr optional or_else(_Func&& __f) const {
1364 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
1365 "Result of f() should be the same type as this optional");
1366 if (*this)
1367 return *this;
1368 return std::forward<_Func>(__f)();
1369 }
1370
1371 using __base::reset;
1372};
1373# endif
1374
1375template <class _Tp>
1376optional(_Tp) -> optional<_Tp>;
1377
1378// [optional.relops] Relational operators
1379
1380template <
1381 class _Tp,
1382 class _Up,
1383 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1384 int> = 0>
1385_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, const optional<_Up>& __y) {
1386 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1387 return false;
1388 if (!static_cast<bool>(__x))
1389 return true;
1390 return *__x == *__y;
1391}
1392
1393template <
1394 class _Tp,
1395 class _Up,
1396 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1397 int> = 0>
1398_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) {
1399 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1400 return true;
1401 if (!static_cast<bool>(__x))
1402 return false;
1403 return *__x != *__y;
1404}
1405
1406template < 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 optional<_Tp>& __x, const optional<_Up>& __y) {
1411 if (!static_cast<bool>(__y))
1412 return false;
1413 if (!static_cast<bool>(__x))
1414 return true;
1415 return *__x < *__y;
1416}
1417
1418template < class _Tp,
1419 class _Up,
1420 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1421 int> = 0>
1422_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, const optional<_Up>& __y) {
1423 if (!static_cast<bool>(__x))
1424 return false;
1425 if (!static_cast<bool>(__y))
1426 return true;
1427 return *__x > *__y;
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 optional<_Up>& __y) {
1436 if (!static_cast<bool>(__x))
1437 return true;
1438 if (!static_cast<bool>(__y))
1439 return false;
1440 return *__x <= *__y;
1441}
1442
1443template <
1444 class _Tp,
1445 class _Up,
1446 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1447 int> = 0>
1448_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) {
1449 if (!static_cast<bool>(__y))
1450 return true;
1451 if (!static_cast<bool>(__x))
1452 return false;
1453 return *__x >= *__y;
1454}
1455
1456# if _LIBCPP_STD_VER >= 20
1457
1458template <class _Tp, three_way_comparable_with<_Tp> _Up>
1459_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
1460operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) {
1461 if (__x && __y)
1462 return *__x <=> *__y;
1463 return __x.has_value() <=> __y.has_value();
1464}
1465
1466# endif // _LIBCPP_STD_VER >= 20
1467
1468// [optional.nullops] Comparison with nullopt
1469
1470template <class _Tp>
1471_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept {
1472 return !static_cast<bool>(__x);
1473}
1474
1475# if _LIBCPP_STD_VER <= 17
1476
1477template <class _Tp>
1478_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(nullopt_t, const optional<_Tp>& __x) noexcept {
1479 return !static_cast<bool>(__x);
1480}
1481
1482template <class _Tp>
1483_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, nullopt_t) noexcept {
1484 return static_cast<bool>(__x);
1485}
1486
1487template <class _Tp>
1488_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(nullopt_t, const optional<_Tp>& __x) noexcept {
1489 return static_cast<bool>(__x);
1490}
1491
1492template <class _Tp>
1493_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>&, nullopt_t) noexcept {
1494 return false;
1495}
1496
1497template <class _Tp>
1498_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(nullopt_t, const optional<_Tp>& __x) noexcept {
1499 return static_cast<bool>(__x);
1500}
1501
1502template <class _Tp>
1503_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, nullopt_t) noexcept {
1504 return !static_cast<bool>(__x);
1505}
1506
1507template <class _Tp>
1508_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(nullopt_t, const optional<_Tp>&) noexcept {
1509 return true;
1510}
1511
1512template <class _Tp>
1513_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, nullopt_t) noexcept {
1514 return static_cast<bool>(__x);
1515}
1516
1517template <class _Tp>
1518_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(nullopt_t, const optional<_Tp>&) noexcept {
1519 return false;
1520}
1521
1522template <class _Tp>
1523_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>&, nullopt_t) noexcept {
1524 return true;
1525}
1526
1527template <class _Tp>
1528_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(nullopt_t, const optional<_Tp>& __x) noexcept {
1529 return !static_cast<bool>(__x);
1530}
1531
1532# else // _LIBCPP_STD_VER <= 17
1533
1534template <class _Tp>
1535_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept {
1536 return __x.has_value() <=> false;
1537}
1538
1539# endif // _LIBCPP_STD_VER <= 17
1540
1541// [optional.comp.with.t] Comparison with T
1542
1543template <
1544 class _Tp,
1545 class _Up,
1546 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1547 int> = 0>
1548_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, const _Up& __v) {
1549 if (__x.has_value())
1550 return *__x == __v;
1551 return false;
1552}
1553
1554template <
1555 class _Tp,
1556 class _Up,
1557 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1558 int> = 0>
1559_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const _Tp& __v, const optional<_Up>& __x) {
1560 if (__x.has_value())
1561 return __v == *__x;
1562 return false;
1563}
1564
1565template <
1566 class _Tp,
1567 class _Up,
1568 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1569 int> = 0>
1570_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, const _Up& __v) {
1571 if (__x.has_value())
1572 return *__x != __v;
1573 return true;
1574}
1575
1576template <
1577 class _Tp,
1578 class _Up,
1579 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1580 int> = 0>
1581_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const _Tp& __v, const optional<_Up>& __x) {
1582 if (__x.has_value())
1583 return __v != *__x;
1584 return true;
1585}
1586
1587template < class _Tp,
1588 class _Up,
1589 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1590 int> = 0>
1591_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>& __x, const _Up& __v) {
1592 if (__x.has_value())
1593 return *__x < __v;
1594 return true;
1595}
1596
1597template < class _Tp,
1598 class _Up,
1599 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1600 int> = 0>
1601_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const _Tp& __v, const optional<_Up>& __x) {
1602 if (__x.has_value())
1603 return __v < *__x;
1604 return false;
1605}
1606
1607template <
1608 class _Tp,
1609 class _Up,
1610 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1611 int> = 0>
1612_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, const _Up& __v) {
1613 if (__x.has_value())
1614 return *__x <= __v;
1615 return true;
1616}
1617
1618template <
1619 class _Tp,
1620 class _Up,
1621 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1622 int> = 0>
1623_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const _Tp& __v, const optional<_Up>& __x) {
1624 if (__x.has_value())
1625 return __v <= *__x;
1626 return false;
1627}
1628
1629template < class _Tp,
1630 class _Up,
1631 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1632 int> = 0>
1633_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, const _Up& __v) {
1634 if (__x.has_value())
1635 return *__x > __v;
1636 return false;
1637}
1638
1639template < class _Tp,
1640 class _Up,
1641 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1642 int> = 0>
1643_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const _Tp& __v, const optional<_Up>& __x) {
1644 if (__x.has_value())
1645 return __v > *__x;
1646 return true;
1647}
1648
1649template <
1650 class _Tp,
1651 class _Up,
1652 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1653 int> = 0>
1654_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>& __x, const _Up& __v) {
1655 if (__x.has_value())
1656 return *__x >= __v;
1657 return false;
1658}
1659
1660template <
1661 class _Tp,
1662 class _Up,
1663 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1664 int> = 0>
1665_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const _Tp& __v, const optional<_Up>& __x) {
1666 if (__x.has_value())
1667 return __v >= *__x;
1668 return true;
1669}
1670
1671# if _LIBCPP_STD_VER >= 20
1672
1673template <class _Tp, class _Up>
1674 requires(!__is_derived_from_optional<_Up>) && three_way_comparable_with<_Tp, _Up>
1675_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
1676operator<=>(const optional<_Tp>& __x, const _Up& __v) {
1677 return __x.has_value() ? *__x <=> __v : strong_ordering::less;
1678}
1679
1680# endif // _LIBCPP_STD_VER >= 20
1681
1682template <class _Tp,
1683 enable_if_t<(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)
1684# if _LIBCPP_STD_VER >= 26
1685 || is_reference_v<_Tp>
1686# endif
1687 ,
1688 int> = 0>
1689inline _LIBCPP_HIDE_FROM_ABI
1690_LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
1691 __x.swap(__y);
1692}
1693
1694struct __make_optional_barrier_tag {
1695 explicit __make_optional_barrier_tag() = default;
1696};
1697
1698template <class _Tp, class... _Args>
1699inline constexpr bool __is_constructible_for_optional_v = is_constructible_v<_Tp, _Args...>;
1700
1701template <class _Tp, class... _Args>
1702struct __is_constructible_for_optional : bool_constant<__is_constructible_for_optional_v<_Tp, _Args...>> {};
1703
1704template <class _Tp, class _Up, class... _Args>
1705inline constexpr bool __is_constructible_for_optional_initializer_list_v =
1706 is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>;
1707
1708# if _LIBCPP_STD_VER >= 26
1709template <class _Tp, class... _Args>
1710inline constexpr bool __is_constructible_for_optional_v<_Tp&, _Args...> = false;
1711
1712template <class _Tp, class _Arg>
1713inline constexpr bool __is_constructible_for_optional_v<_Tp&, _Arg> =
1714 is_constructible_v<_Tp&, _Arg> && !reference_constructs_from_temporary_v<_Tp&, _Arg>;
1715
1716template <class _Tp, class _Up, class... _Args>
1717inline constexpr bool __is_constructible_for_optional_initializer_list_v<_Tp&, _Up, _Args...> = false;
1718# endif
1719
1720template <
1721# if _LIBCPP_STD_VER >= 26
1722 __make_optional_barrier_tag = __make_optional_barrier_tag{},
1723# endif
1724 class _Tp,
1725 enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>, int> = 0>
1726[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
1727 return optional<decay_t<_Tp>>(std::forward<_Tp>(__v));
1728}
1729
1730template <class _Tp, class... _Args, enable_if_t<__is_constructible_for_optional_v<_Tp, _Args...>, int> = 0>
1731[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
1732 return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
1733}
1734
1735template <class _Tp,
1736 class _Up,
1737 class... _Args,
1738 enable_if_t<__is_constructible_for_optional_initializer_list_v<_Tp, _Up, _Args...>, int> = 0>
1739[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp>
1740make_optional(initializer_list<_Up> __il, _Args&&... __args) {
1741 return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
1742}
1743
1744template <class _Tp>
1745struct hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > {
1746# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1747 _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type;
1748 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
1749# endif
1750
1751 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
1752 return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
1753 }
1754};
1755
1756_LIBCPP_END_NAMESPACE_STD
1757
1758# endif // _LIBCPP_STD_VER >= 17
1759
1760_LIBCPP_POP_MACROS
1761
1762# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
1763# include <atomic>
1764# include <climits>
1765# include <concepts>
1766# include <ctime>
1767# include <iterator>
1768# include <limits>
1769# include <memory>
1770# include <ratio>
1771# include <stdexcept>
1772# include <tuple>
1773# include <type_traits>
1774# include <typeinfo>
1775# include <utility>
1776# include <variant>
1777# endif
1778#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1779
1780#endif // _LIBCPP_OPTIONAL
1781