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 <__cstddef/ptrdiff_t.h>
268# include <__exception/exception.h>
269# include <__format/range_format.h>
270# include <__functional/hash.h>
271# include <__functional/invoke.h>
272# include <__functional/unary_function.h>
273# include <__fwd/functional.h>
274# include <__iterator/bounded_iter.h>
275# include <__iterator/wrap_iter.h>
276# include <__memory/addressof.h>
277# include <__memory/construct_at.h>
278# include <__ranges/enable_borrowed_range.h>
279# include <__ranges/enable_view.h>
280# include <__tuple/sfinae_helpers.h>
281# include <__type_traits/add_pointer.h>
282# include <__type_traits/conditional.h>
283# include <__type_traits/conjunction.h>
284# include <__type_traits/decay.h>
285# include <__type_traits/disjunction.h>
286# include <__type_traits/enable_if.h>
287# include <__type_traits/integral_constant.h>
288# include <__type_traits/invoke.h>
289# include <__type_traits/is_array.h>
290# include <__type_traits/is_assignable.h>
291# include <__type_traits/is_constructible.h>
292# include <__type_traits/is_convertible.h>
293# include <__type_traits/is_core_convertible.h>
294# include <__type_traits/is_destructible.h>
295# include <__type_traits/is_function.h>
296# include <__type_traits/is_nothrow_assignable.h>
297# include <__type_traits/is_nothrow_constructible.h>
298# include <__type_traits/is_object.h>
299# include <__type_traits/is_reference.h>
300# include <__type_traits/is_same.h>
301# include <__type_traits/is_scalar.h>
302# include <__type_traits/is_swappable.h>
303# include <__type_traits/is_trivially_assignable.h>
304# include <__type_traits/is_trivially_constructible.h>
305# include <__type_traits/is_trivially_destructible.h>
306# include <__type_traits/is_trivially_relocatable.h>
307# include <__type_traits/negation.h>
308# include <__type_traits/reference_constructs_from_temporary.h>
309# include <__type_traits/remove_const.h>
310# include <__type_traits/remove_cv.h>
311# include <__type_traits/remove_cvref.h>
312# include <__type_traits/remove_reference.h>
313# include <__utility/declval.h>
314# include <__utility/forward.h>
315# include <__utility/in_place.h>
316# include <__utility/move.h>
317# include <__utility/swap.h>
318# include <__verbose_abort>
319# include <initializer_list>
320# include <version>
321
322// standard-mandated includes
323
324// [optional.syn]
325# include <compare>
326
327# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
328# pragma GCC system_header
329# endif
330
331_LIBCPP_PUSH_MACROS
332# include <__undef_macros>
333
334namespace std // purposefully not using versioning namespace
335{
336
337class _LIBCPP_EXPORTED_FROM_ABI bad_optional_access : public exception {
338public:
339 _LIBCPP_HIDE_FROM_ABI bad_optional_access() _NOEXCEPT = default;
340 _LIBCPP_HIDE_FROM_ABI bad_optional_access(const bad_optional_access&) _NOEXCEPT = default;
341 _LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default;
342 // Get the key function ~bad_optional_access() into the dylib
343 ~bad_optional_access() _NOEXCEPT override;
344 [[__nodiscard__]] const char* what() const _NOEXCEPT override;
345};
346
347} // namespace std
348
349# if _LIBCPP_STD_VER >= 17
350
351_LIBCPP_BEGIN_NAMESPACE_STD
352
353[[noreturn]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_optional_access() {
354# if _LIBCPP_HAS_EXCEPTIONS
355 throw bad_optional_access();
356# else
357 _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode");
358# endif
359}
360
361struct nullopt_t {
362 struct __secret_tag {
363 explicit __secret_tag() = default;
364 };
365 _LIBCPP_HIDE_FROM_ABI constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
366};
367
368inline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
369
370struct __optional_construct_from_invoke_tag {};
371
372template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
373struct __optional_destruct_base;
374
375template <class _Tp>
376struct __optional_destruct_base<_Tp, false> {
377 typedef _Tp value_type;
378 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
379 union {
380 char __null_state_;
381 remove_cv_t<value_type> __val_;
382 };
383 bool __engaged_;
384
385 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__optional_destruct_base() {
386 if (__engaged_)
387 __val_.~value_type();
388 }
389
390 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
391
392 template <class... _Args>
393 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
394 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
395
396# if _LIBCPP_STD_VER >= 23
397 template <class _Fp, class... _Args>
398 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(
399 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
400 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
401# endif
402
403 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
404 if (__engaged_) {
405 __val_.~value_type();
406 __engaged_ = false;
407 }
408 }
409};
410
411template <class _Tp>
412struct __optional_destruct_base<_Tp, true> {
413 typedef _Tp value_type;
414 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
415 union {
416 char __null_state_;
417 remove_cv_t<value_type> __val_;
418 };
419 bool __engaged_;
420
421 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
422
423 template <class... _Args>
424 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
425 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
426
427# if _LIBCPP_STD_VER >= 23
428 template <class _Fp, class... _Args>
429 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base(
430 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
431 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
432# endif
433
434 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
435 if (__engaged_) {
436 __engaged_ = false;
437 }
438 }
439};
440
441template <class _Tp, bool = is_reference<_Tp>::value>
442struct __optional_storage_base : __optional_destruct_base<_Tp> {
443 using __base _LIBCPP_NODEBUG = __optional_destruct_base<_Tp>;
444 using value_type = _Tp;
445 using __base::__base;
446
447 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; }
448
449 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; }
450 _LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; }
451 _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() && noexcept { return std::move(this->__val_); }
452 _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& __get() const&& noexcept { return std::move(this->__val_); }
453
454 template <class... _Args>
455 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_Args&&... __args) {
456 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
457 std::__construct_at(std::addressof(this->__val_), std::forward<_Args>(__args)...);
458 this->__engaged_ = true;
459 }
460
461 template <class _That>
462 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
463 if (__opt.has_value())
464 __construct(std::forward<_That>(__opt).__get());
465 }
466
467 template <class _That>
468 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
469 if (this->__engaged_ == __opt.has_value()) {
470 if (this->__engaged_)
471 static_cast<_Tp&>(this->__val_) = std::forward<_That>(__opt).__get();
472 } else {
473 if (this->__engaged_)
474 this->reset();
475 else
476 __construct(std::forward<_That>(__opt).__get());
477 }
478 }
479
480 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
481 __swap(__optional_storage_base& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>) {
482 using std::swap;
483 if (this->has_value() == __rhs.has_value()) {
484 if (this->has_value())
485 swap(this->__get(), __rhs.__get());
486 } else {
487 if (this->has_value()) {
488 __rhs.__construct(std::move(this->__get()));
489 this->reset();
490 } else {
491 this->__construct(std::move(__rhs.__get()));
492 __rhs.reset();
493 }
494 }
495 }
496};
497
498template <class _Tp>
499struct __optional_storage_base<_Tp, true> {
500 using value_type = _Tp;
501 using __raw_type _LIBCPP_NODEBUG = remove_reference_t<_Tp>;
502 __raw_type* __value_;
503
504 _LIBCPP_HIDE_FROM_ABI constexpr __optional_storage_base() noexcept : __value_(nullptr) {}
505
506 template <class _Up>
507 _LIBCPP_HIDE_FROM_ABI constexpr void __convert_init_ref_val(_Up&& __val) noexcept {
508 _Tp& __r(std::forward<_Up>(__val));
509 __value_ = std::addressof(__r);
510 }
511
512 template <class _UArg>
513 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg) {
514 static_assert(!__reference_constructs_from_temporary_v<_Tp, _UArg>,
515 "Attempted to construct a reference element in optional from a "
516 "possible temporary");
517 __convert_init_ref_val(std::forward<_UArg>(__uarg));
518 }
519
520 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; }
521
522 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; }
523
524 _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const noexcept { return *__value_; }
525
526 template <class _UArg>
527 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_UArg&& __val) {
528 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
529 static_assert(!__reference_constructs_from_temporary_v<_Tp, _UArg>,
530 "Attempted to construct a reference element in tuple from a "
531 "possible temporary");
532 __convert_init_ref_val(std::forward<_UArg>(__val));
533 }
534
535 template <class _That>
536 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
537 if (__opt.has_value())
538 __construct(std::forward<_That>(__opt).__get());
539 }
540
541 template <class _That>
542 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
543 if (has_value() == __opt.has_value()) {
544 if (has_value())
545 *__value_ = std::forward<_That>(__opt).__get();
546 } else {
547 if (has_value())
548 reset();
549 else
550 __construct(std::forward<_That>(__opt).__get());
551 }
552 }
553
554 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__optional_storage_base& __rhs) noexcept {
555 std::swap(__value_, __rhs.__value_);
556 }
557};
558
559template <class _Tp, bool = is_trivially_copy_constructible_v<_Tp> || is_lvalue_reference_v<_Tp>>
560struct __optional_copy_base : __optional_storage_base<_Tp> {
561 using __optional_storage_base<_Tp>::__optional_storage_base;
562};
563
564template <class _Tp>
565struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> {
566 using __optional_storage_base<_Tp>::__optional_storage_base;
567
568 _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default;
569
570 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) {
571 this->__construct_from(__opt);
572 }
573
574 _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&) = default;
575 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default;
576 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&) = default;
577};
578
579template <class _Tp, bool = is_trivially_move_constructible_v<_Tp> || is_lvalue_reference_v<_Tp>>
580struct __optional_move_base : __optional_copy_base<_Tp> {
581 using __optional_copy_base<_Tp>::__optional_copy_base;
582};
583
584template <class _Tp>
585struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> {
586 using value_type = _Tp;
587 using __optional_copy_base<_Tp>::__optional_copy_base;
588
589 _LIBCPP_HIDE_FROM_ABI __optional_move_base() = default;
590 _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default;
591
592 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
593 __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) {
594 this->__construct_from(std::move(__opt));
595 }
596
597 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default;
598 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&) = default;
599};
600
601template <class _Tp,
602 bool = (is_trivially_destructible_v<_Tp> && is_trivially_copy_constructible_v<_Tp> &&
603 is_trivially_copy_assignable_v<_Tp>) ||
604 is_lvalue_reference_v<_Tp>>
605struct __optional_copy_assign_base : __optional_move_base<_Tp> {
606 using __optional_move_base<_Tp>::__optional_move_base;
607};
608
609template <class _Tp>
610struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> {
611 using __optional_move_base<_Tp>::__optional_move_base;
612
613 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base() = default;
614 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
615 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&) = default;
616
617 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base&
618 operator=(const __optional_copy_assign_base& __opt) {
619 this->__assign_from(__opt);
620 return *this;
621 }
622
623 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
624};
625
626template <class _Tp,
627 bool = (is_trivially_destructible_v<_Tp> && is_trivially_move_constructible_v<_Tp> &&
628 is_trivially_move_assignable_v<_Tp>) ||
629 is_lvalue_reference_v<_Tp>>
630struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> {
631 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
632};
633
634template <class _Tp>
635struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> {
636 using value_type = _Tp;
637 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
638
639 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base() = default;
640 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;
641 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&) = default;
642 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
643
644 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base&
645 operator=(__optional_move_assign_base&& __opt) noexcept(
646 is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) {
647 this->__assign_from(std::move(__opt));
648 return *this;
649 }
650};
651
652template <class _Tp>
653using __optional_sfinae_ctor_base_t _LIBCPP_NODEBUG =
654 __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >;
655
656template <class _Tp>
657using __optional_sfinae_assign_base_t _LIBCPP_NODEBUG =
658 __sfinae_assign_base< (is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Tp>) || is_lvalue_reference_v<_Tp>,
659 (is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp>) || is_lvalue_reference_v<_Tp>>;
660
661template <class _Tp>
662class optional;
663
664# if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
665template <class _Tp>
666constexpr bool ranges::enable_view<optional<_Tp>> = true;
667
668template <class _Tp>
669constexpr range_format format_kind<optional<_Tp>> = range_format::disabled;
670
671template <class _Tp>
672constexpr bool ranges::enable_borrowed_range<optional<_Tp&>> = true;
673
674# endif
675
676# if _LIBCPP_STD_VER >= 20
677
678template <class _Tp>
679concept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(const optional<_Up>&) {}(__t); };
680
681# endif // _LIBCPP_STD_VER >= 20
682
683template <class _Tp>
684struct __is_std_optional : false_type {};
685template <class _Tp>
686struct __is_std_optional<optional<_Tp>> : true_type {};
687
688template <class _Tp, class... _Args>
689inline constexpr bool __is_constructible_for_optional_v = is_constructible_v<_Tp, _Args...>;
690
691template <class _Tp, class... _Args>
692struct __is_constructible_for_optional : bool_constant<__is_constructible_for_optional_v<_Tp, _Args...>> {};
693
694template <class _Tp, class _Up, class... _Args>
695inline constexpr bool __is_constructible_for_optional_initializer_list_v =
696 is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>;
697
698# if _LIBCPP_STD_VER >= 26
699template <class _Tp, class... _Args>
700inline constexpr bool __is_constructible_for_optional_v<_Tp&, _Args...> = false;
701template <class _Tp, class _Arg>
702inline constexpr bool __is_constructible_for_optional_v<_Tp&, _Arg> =
703 is_constructible_v<_Tp&, _Arg> && !reference_constructs_from_temporary_v<_Tp&, _Arg>;
704
705template <class _Tp, class _Up, class... _Args>
706inline constexpr bool __is_constructible_for_optional_initializer_list_v<_Tp&, _Up, _Args...> = false;
707# endif
708
709template <class _Tp, class = void>
710struct __optional_iterator {};
711
712# if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
713
714template <class _Tp>
715struct __optional_iterator<_Tp, enable_if_t<is_object_v<_Tp>>> {
716private:
717 using __pointer _LIBCPP_NODEBUG = add_pointer_t<_Tp>;
718 using __const_pointer _LIBCPP_NODEBUG = add_pointer_t<const _Tp>;
719
720public:
721# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
722 using iterator = __bounded_iter<__wrap_iter<__pointer>>;
723 using const_iterator = __bounded_iter<__wrap_iter<__const_pointer>>;
724# else
725 using iterator = __wrap_iter<__pointer>;
726 using const_iterator = __wrap_iter<__const_pointer>;
727# endif
728
729 // [optional.iterators], iterator support
730 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {
731 auto& __derived_self = static_cast<optional<_Tp>&>(*this);
732 auto* __ptr = std::addressof(__derived_self.__get());
733
734# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
735 return std::__make_bounded_iter(
736 __wrap_iter<__pointer>(__ptr),
737 __wrap_iter<__pointer>(__ptr),
738 __wrap_iter<__pointer>(__ptr) + (__derived_self.has_value() ? 1 : 0));
739# else
740 return iterator(__ptr);
741# endif
742 }
743
744 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {
745 auto& __derived_self = static_cast<const optional<_Tp>&>(*this);
746 auto* __ptr = std::addressof(__derived_self.__get());
747
748# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
749 return std::__make_bounded_iter(
750 __wrap_iter<__const_pointer>(__ptr),
751 __wrap_iter<__const_pointer>(__ptr),
752 __wrap_iter<__const_pointer>(__ptr) + (__derived_self.has_value() ? 1 : 0));
753# else
754 return const_iterator(__ptr);
755# endif
756 }
757
758 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept {
759 return begin() + (static_cast<optional<_Tp>&>(*this).has_value() ? 1 : 0);
760 }
761 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept {
762 return begin() + (static_cast<const optional<_Tp>&>(*this).has_value() ? 1 : 0);
763 }
764};
765
766template <class _Tp>
767struct __optional_iterator<_Tp&, enable_if_t<is_object_v<_Tp> && !__is_unbounded_array_v<_Tp> >> {
768private:
769 using __pointer _LIBCPP_NODEBUG = add_pointer_t<_Tp>;
770
771public:
772# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
773 using iterator = __bounded_iter<__wrap_iter<__pointer>>;
774# else
775 using iterator = __wrap_iter<__pointer>;
776# endif
777
778 // [optional.ref.iterators], iterator support
779
780 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const noexcept {
781 auto& __derived_self = static_cast<const optional<_Tp&>&>(*this);
782 auto* __ptr = __derived_self.has_value() ? std::addressof(__derived_self.__get()) : nullptr;
783
784# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
785 return std::__make_bounded_iter(
786 __wrap_iter<__pointer>(__ptr),
787 __wrap_iter<__pointer>(__ptr),
788 __wrap_iter<__pointer>(__ptr) + (__derived_self.has_value() ? 1 : 0));
789# else
790 return iterator(__ptr);
791# endif
792 }
793
794 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const noexcept {
795 return begin() + (static_cast<const optional<_Tp&>&>(*this).has_value() ? 1 : 0);
796 }
797};
798
799# endif // _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
800
801template <class _Tp>
802class _LIBCPP_DECLSPEC_EMPTY_BASES optional
803 : private __optional_move_assign_base<_Tp>,
804 private __optional_sfinae_ctor_base_t<_Tp>,
805 private __optional_sfinae_assign_base_t<_Tp>,
806 public __optional_iterator<_Tp> {
807 using __base _LIBCPP_NODEBUG = __optional_move_assign_base<_Tp>;
808
809public:
810 using value_type = __libcpp_remove_reference_t<_Tp>;
811
812 using __trivially_relocatable _LIBCPP_NODEBUG =
813 conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, optional, void>;
814
815private:
816 static_assert(!is_same_v<__remove_cvref_t<_Tp>, in_place_t>,
817 "instantiation of optional with in_place_t is ill-formed");
818 static_assert(!is_same_v<__remove_cvref_t<_Tp>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed");
819# if _LIBCPP_STD_VER >= 26
820 static_assert(!is_rvalue_reference_v<_Tp>, "instantiation of optional with an rvalue reference type is ill-formed");
821# else
822 static_assert(!is_reference_v<_Tp>, "instantiation of optional with a reference type is ill-formed");
823# endif
824 static_assert(is_destructible_v<_Tp>, "instantiation of optional with a non-destructible type is ill-formed");
825 static_assert(!is_array_v<_Tp>, "instantiation of optional with an array type is ill-formed");
826
827# if _LIBCPP_STD_VER >= 26
828 template <class _Up>
829 constexpr static bool __libcpp_opt_ref_ctor_deleted =
830 is_lvalue_reference_v<_Tp> && reference_constructs_from_temporary_v<_Tp, _Up>;
831
832 template <class _Up>
833 constexpr static bool __ref_ctor_enabled =
834 is_lvalue_reference_v<_Tp> && !reference_constructs_from_temporary_v<_Tp, _Up>;
835# endif
836
837 // LWG2756: conditionally explicit conversion from _Up
838 struct _CheckOptionalArgsConstructor {
839 template <class _Up>
840 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
841 return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>;
842 }
843
844 template <class _Up>
845 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
846 return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>;
847 }
848 };
849 template <class _Up>
850 using _CheckOptionalArgsCtor _LIBCPP_NODEBUG =
851 _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value &&
852 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value),
853 _CheckOptionalArgsConstructor,
854 __check_tuple_constructor_fail >;
855 template <class _QualUp>
856 struct _CheckOptionalLikeConstructor {
857 template <class _Up, class _Opt = optional<_Up>>
858 using __check_constructible_from_opt _LIBCPP_NODEBUG =
859 _Or< is_constructible<_Tp, _Opt&>,
860 is_constructible<_Tp, _Opt const&>,
861 is_constructible<_Tp, _Opt&&>,
862 is_constructible<_Tp, _Opt const&&>,
863 is_convertible<_Opt&, _Tp>,
864 is_convertible<_Opt const&, _Tp>,
865 is_convertible<_Opt&&, _Tp>,
866 is_convertible<_Opt const&&, _Tp> >;
867 template <class _Up, class _Opt = optional<_Up>>
868 using __check_assignable_from_opt _LIBCPP_NODEBUG =
869 _Or< is_assignable<_Tp&, _Opt&>,
870 is_assignable<_Tp&, _Opt const&>,
871 is_assignable<_Tp&, _Opt&&>,
872 is_assignable<_Tp&, _Opt const&&> >;
873 template <class _Up, class _QUp = _QualUp>
874 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
875 return is_convertible<_QUp, _Tp>::value &&
876 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
877 }
878 template <class _Up, class _QUp = _QualUp>
879 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
880 return !is_convertible<_QUp, _Tp>::value &&
881 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
882 }
883 template <class _Up, class _QUp = _QualUp>
884 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() {
885 // Construction and assignability of _QUp to _Tp has already been
886 // checked.
887 return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value;
888 }
889 };
890
891 template <class _Up, class _QualUp>
892 using _CheckOptionalLikeCtor _LIBCPP_NODEBUG =
893 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value,
894 _CheckOptionalLikeConstructor<_QualUp>,
895 __check_tuple_constructor_fail >;
896 template <class _Up, class _QualUp>
897 using _CheckOptionalLikeAssign _LIBCPP_NODEBUG =
898 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value,
899 _CheckOptionalLikeConstructor<_QualUp>,
900 __check_tuple_constructor_fail >;
901
902public:
903 _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {}
904 _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default;
905 _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&) = default;
906 _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {}
907
908 template <
909 class _InPlaceT,
910 class... _Args,
911 enable_if_t<_And<_IsSame<_InPlaceT, in_place_t>, __is_constructible_for_optional<_Tp, _Args...>>::value, int> = 0>
912 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args)
913 : __base(in_place, std::forward<_Args>(__args)...) {}
914
915 template <class _Up,
916 class... _Args,
917 enable_if_t<__is_constructible_for_optional_initializer_list_v<_Tp, _Up, _Args...>, int> = 0>
918 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
919 : __base(in_place, __il, std::forward<_Args>(__args)...) {}
920
921 template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
922 _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v)
923# if _LIBCPP_STD_VER >= 26
924 noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up>)
925# endif
926 : __base(in_place, std::forward<_Up>(__v)) {
927 }
928
929 template <class _Up = remove_cv_t<_Tp>,
930 enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>
931 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v)
932# if _LIBCPP_STD_VER >= 26
933 noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up>)
934# endif
935 : __base(in_place, std::forward<_Up>(__v)) {
936 }
937
938 // LWG2756: conditionally explicit conversion from const optional<_Up>&
939 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
940 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v)
941# if _LIBCPP_STD_VER >= 26
942 noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, const _Up&>)
943# endif
944 {
945 this->__construct_from(__v);
946 }
947 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
948 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v)
949# if _LIBCPP_STD_VER >= 26
950 noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, const _Up&>)
951# endif
952 {
953 this->__construct_from(__v);
954 }
955
956 // LWG2756: conditionally explicit conversion from optional<_Up>&&
957 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>
958 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v)
959# if _LIBCPP_STD_VER >= 26
960 noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up>)
961# endif
962 {
963 this->__construct_from(std::move(__v));
964 }
965 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>
966 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v)
967# if _LIBCPP_STD_VER >= 26
968 noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up>)
969# endif
970 {
971 this->__construct_from(std::move(__v));
972 }
973
974 // deleted optional<T&> constructors and additional optional<T&> constructors
975# if _LIBCPP_STD_VER >= 26
976 // optional(U&&)
977 template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
978 requires __libcpp_opt_ref_ctor_deleted<_Up>
979 optional(_Up&&) = delete;
980
981 template <class _Up = remove_cv_t<_Tp>,
982 enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>
983 requires __libcpp_opt_ref_ctor_deleted<_Up>
984 explicit optional(_Up&&) = delete;
985
986 // optional(optional<U>& rhs)
987 template <class _Up>
988 requires __ref_ctor_enabled<_Up&> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) && (!is_same_v<_Tp&, _Up>) &&
989 is_constructible_v<_Tp&, _Up&>
990 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up&, _Tp&>)
991 optional(optional<_Up>& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, _Up&>) {
992 this->__construct_from(__rhs);
993 }
994
995 template <class _Up>
996 requires __libcpp_opt_ref_ctor_deleted<_Up&> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
997 (!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up&>
998 constexpr explicit optional(optional<_Up>& __rhs) noexcept = delete;
999
1000 // optional(const optional<U>&)
1001 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
1002 requires __libcpp_opt_ref_ctor_deleted<const _Up&>
1003 optional(const optional<_Up>&) = delete;
1004
1005 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
1006 requires __libcpp_opt_ref_ctor_deleted<const _Up&>
1007 explicit optional(const optional<_Up>&) = delete;
1008
1009 // optional(optional<U>&&)
1010 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>
1011 requires __libcpp_opt_ref_ctor_deleted<_Up>
1012 optional(optional<_Up>&&) = delete;
1013
1014 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>
1015 requires __libcpp_opt_ref_ctor_deleted<_Up>
1016 explicit optional(optional<_Up>&&) = delete;
1017
1018 // optional(const optional<U>&&)
1019 template <class _Up>
1020 requires __ref_ctor_enabled<const _Up> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
1021 (!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, const _Up>
1022 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(!is_convertible_v<const _Up, _Tp&>)
1023 optional(const optional<_Up>&& __v) noexcept(is_nothrow_constructible_v<_Tp&, const _Up>) {
1024 this->__construct_from(std::move(__v));
1025 }
1026
1027 template <class _Up>
1028 requires __libcpp_opt_ref_ctor_deleted<const _Up> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) &&
1029 (!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, const _Up>
1030 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>&& __v) noexcept = delete;
1031# endif
1032
1033# if _LIBCPP_STD_VER >= 23
1034 template <class _Tag,
1035 class _Fp,
1036 class... _Args,
1037 enable_if_t<_IsSame<_Tag, __optional_construct_from_invoke_tag>::value, int> = 0>
1038 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Tag, _Fp&& __f, _Args&&... __args)
1039 : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}
1040# endif
1041
1042 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept {
1043 reset();
1044 return *this;
1045 }
1046
1047 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default;
1048 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default;
1049
1050 // LWG2756
1051 template <class _Up = remove_cv_t<_Tp>,
1052 enable_if_t<_And<_IsNotSame<__remove_cvref_t<_Up>, optional>,
1053 _Or<_IsNotSame<__remove_cvref_t<_Up>, _Tp>, _Not<is_scalar<_Tp>>>,
1054 is_constructible<_Tp, _Up>,
1055 is_assignable<_Tp&, _Up>,
1056 is_object<_Tp>>::value,
1057 int> = 0>
1058 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) {
1059 if (this->has_value())
1060 this->__get() = std::forward<_Up>(__v);
1061 else
1062 this->__construct(std::forward<_Up>(__v));
1063 return *this;
1064 }
1065
1066 // LWG2756
1067 template <class _Up, enable_if_t<_CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0>
1068 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) {
1069 this->__assign_from(__v);
1070 return *this;
1071 }
1072
1073 // LWG2756
1074 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0>
1075 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) {
1076 this->__assign_from(std::move(__v));
1077 return *this;
1078 }
1079
1080 template <class... _Args, enable_if_t<__is_constructible_for_optional_v<_Tp, _Args...>, int> = 0>
1081 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args)
1082# if _LIBCPP_STD_VER >= 26
1083 noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp, _Args...>)
1084# endif
1085 {
1086 reset();
1087 this->__construct(std::forward<_Args>(__args)...);
1088 return this->__get();
1089 }
1090
1091 template <class _Up,
1092 class... _Args,
1093 enable_if_t<__is_constructible_for_optional_initializer_list_v<_Tp, _Up, _Args...>, int> = 0>
1094 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1095 reset();
1096 this->__construct(__il, std::forward<_Args>(__args)...);
1097 return this->__get();
1098 }
1099
1100 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(optional& __opt) noexcept(
1101 (is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>)
1102# if _LIBCPP_STD_VER >= 26
1103 || is_lvalue_reference_v<_Tp>
1104# endif
1105 ) {
1106 this->__swap(__opt);
1107 }
1108
1109 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp const> operator->() const noexcept
1110# if _LIBCPP_STD_VER >= 26
1111 requires(is_object_v<_Tp>)
1112# endif
1113 {
1114 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
1115 return std::addressof(this->__get());
1116 }
1117
1118 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() noexcept
1119# if _LIBCPP_STD_VER >= 26
1120 requires(is_object_v<_Tp>)
1121# endif
1122 {
1123 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
1124 return std::addressof(this->__get());
1125 }
1126
1127 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept
1128# if _LIBCPP_STD_VER >= 26
1129 requires(is_object_v<_Tp>)
1130# endif
1131 {
1132 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
1133 return this->__get();
1134 }
1135
1136 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept
1137# if _LIBCPP_STD_VER >= 26
1138 requires(is_object_v<_Tp>)
1139# endif
1140 {
1141 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
1142 return this->__get();
1143 }
1144
1145 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept
1146# if _LIBCPP_STD_VER >= 26
1147 requires(is_object_v<_Tp>)
1148# endif
1149 {
1150 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
1151 return std::move(this->__get());
1152 }
1153
1154 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept
1155# if _LIBCPP_STD_VER >= 26
1156 requires(is_object_v<_Tp>)
1157# endif
1158 {
1159 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
1160 return std::move(this->__get());
1161 }
1162
1163 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); }
1164
1165 using __base::__get;
1166 using __base::has_value;
1167
1168 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const&
1169# if _LIBCPP_STD_VER >= 26
1170 requires(is_object_v<_Tp>)
1171# endif
1172 {
1173 if (!this->has_value())
1174 std::__throw_bad_optional_access();
1175 return this->__get();
1176 }
1177
1178 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() &
1179# if _LIBCPP_STD_VER >= 26
1180 requires(is_object_v<_Tp>)
1181# endif
1182 {
1183 if (!this->has_value())
1184 std::__throw_bad_optional_access();
1185 return this->__get();
1186 }
1187
1188 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() &&
1189# if _LIBCPP_STD_VER >= 26
1190 requires(is_object_v<_Tp>)
1191# endif
1192 {
1193 if (!this->has_value())
1194 std::__throw_bad_optional_access();
1195 return std::move(this->__get());
1196 }
1197
1198 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&&
1199# if _LIBCPP_STD_VER >= 26
1200 requires(is_object_v<_Tp>)
1201# endif
1202 {
1203 if (!this->has_value())
1204 std::__throw_bad_optional_access();
1205 return std::move(this->__get());
1206 }
1207
1208 template <class _Up = remove_cv_t<_Tp>>
1209# if _LIBCPP_STD_VER >= 26
1210 requires(is_object_v<_Tp>)
1211# endif
1212 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
1213 static_assert(is_copy_constructible_v<_Tp>, "optional<T>::value_or: T must be copy constructible");
1214 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
1215 return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));
1216 }
1217
1218 template <class _Up = remove_cv_t<_Tp>>
1219# if _LIBCPP_STD_VER >= 26
1220 requires(is_object_v<_Tp>)
1221# endif
1222 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
1223 static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
1224 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
1225 return this->has_value() ? std::move(this->__get()) : static_cast<_Tp>(std::forward<_Up>(__v));
1226 }
1227
1228# if _LIBCPP_STD_VER >= 23
1229 template <class _Func>
1230# if _LIBCPP_STD_VER >= 26
1231 requires(is_object_v<_Tp>)
1232# endif
1233 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
1234 using _Up = invoke_result_t<_Func, _Tp&>;
1235 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1236 "Result of f(value()) must be a specialization of std::optional");
1237 if (*this)
1238 return std::invoke(std::forward<_Func>(__f), value());
1239 return remove_cvref_t<_Up>();
1240 }
1241
1242 template <class _Func>
1243# if _LIBCPP_STD_VER >= 26
1244 requires(is_object_v<_Tp>)
1245# endif
1246 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
1247 using _Up = invoke_result_t<_Func, const _Tp&>;
1248 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1249 "Result of f(value()) must be a specialization of std::optional");
1250 if (*this)
1251 return std::invoke(std::forward<_Func>(__f), value());
1252 return remove_cvref_t<_Up>();
1253 }
1254
1255 template <class _Func>
1256# if _LIBCPP_STD_VER >= 26
1257 requires(is_object_v<_Tp>)
1258# endif
1259 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
1260 using _Up = invoke_result_t<_Func, _Tp&&>;
1261 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1262 "Result of f(std::move(value())) must be a specialization of std::optional");
1263 if (*this)
1264 return std::invoke(std::forward<_Func>(__f), std::move(value()));
1265 return remove_cvref_t<_Up>();
1266 }
1267
1268 template <class _Func>
1269# if _LIBCPP_STD_VER >= 26
1270 requires(is_object_v<_Tp>)
1271# endif
1272 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
1273 using _Up = invoke_result_t<_Func, const _Tp&&>;
1274 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1275 "Result of f(std::move(value())) must be a specialization of std::optional");
1276 if (*this)
1277 return std::invoke(std::forward<_Func>(__f), std::move(value()));
1278 return remove_cvref_t<_Up>();
1279 }
1280
1281 template <class _Func>
1282# if _LIBCPP_STD_VER >= 26
1283 requires(is_object_v<_Tp>)
1284# endif
1285 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1286 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
1287 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
1288 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
1289 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
1290 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
1291 if (*this)
1292 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
1293 return optional<_Up>();
1294 }
1295
1296 template <class _Func>
1297# if _LIBCPP_STD_VER >= 26
1298 requires(is_object_v<_Tp>)
1299# endif
1300 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1301 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;
1302 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
1303 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
1304 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
1305 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
1306 if (*this)
1307 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
1308 return optional<_Up>();
1309 }
1310
1311 template <class _Func>
1312# if _LIBCPP_STD_VER >= 26
1313 requires(is_object_v<_Tp>)
1314# endif
1315 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1316 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;
1317 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
1318 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
1319 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
1320 static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
1321 if (*this)
1322 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
1323 return optional<_Up>();
1324 }
1325
1326 template <class _Func>
1327# if _LIBCPP_STD_VER >= 26
1328 requires(is_object_v<_Tp>)
1329# endif
1330 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1331 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&&>>;
1332 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
1333 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
1334 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
1335 static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
1336 if (*this)
1337 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
1338 return optional<_Up>();
1339 }
1340
1341 template <invocable _Func>
1342 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
1343 requires is_copy_constructible_v<_Tp>
1344# if _LIBCPP_STD_VER >= 26
1345 && (is_object_v<_Tp>)
1346# endif
1347 {
1348 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
1349 "Result of f() should be the same type as this optional");
1350 if (*this)
1351 return *this;
1352 return std::forward<_Func>(__f)();
1353 }
1354
1355 template <invocable _Func>
1356 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
1357 requires is_move_constructible_v<_Tp>
1358# if _LIBCPP_STD_VER >= 26
1359 && (is_object_v<_Tp>)
1360# endif
1361 {
1362 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
1363 "Result of f() should be the same type as this optional");
1364 if (*this)
1365 return std::move(*this);
1366 return std::forward<_Func>(__f)();
1367 }
1368# endif // _LIBCPP_STD_VER >= 23
1369
1370 using __base::reset;
1371
1372// optional<T&> overloads
1373# if _LIBCPP_STD_VER >= 26
1374
1375 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() const noexcept
1376 requires(is_lvalue_reference_v<_Tp>)
1377 {
1378 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
1379 return std::addressof(this->__get());
1380 }
1381
1382 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() const noexcept
1383 requires(is_lvalue_reference_v<_Tp>)
1384 {
1385 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
1386 return this->__get();
1387 }
1388
1389 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() const
1390 requires(is_lvalue_reference_v<_Tp>)
1391 {
1392 if (!this->has_value())
1393 std::__throw_bad_optional_access();
1394 return this->__get();
1395 }
1396
1397 template <class _Up = remove_cvref_t<_Tp>>
1398 requires(is_lvalue_reference_v<_Tp> && is_object_v<__libcpp_remove_reference_t<_Tp>> &&
1399 !is_array_v<__libcpp_remove_reference_t<_Tp>>)
1400 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decay_t<_Tp> value_or(_Up&& __v) const {
1401 static_assert(
1402 is_constructible_v<remove_cvref_t<_Tp>, _Tp&>, "optional<T&>::value_or: remove_cv_t<T> must be constructible");
1403 static_assert(
1404 is_convertible_v<_Up, remove_cvref_t<_Tp>>, "optional<T&>::value_or: U must be convertible to remove_cv_t<T>");
1405 return this->has_value() ? this->__get() : static_cast<remove_cvref_t<_Tp>>(std::forward<_Up>(__v));
1406 }
1407
1408 template <class _Func>
1409 requires(is_lvalue_reference_v<_Tp>)
1410 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const {
1411 using _Up = invoke_result_t<_Func, _Tp&>;
1412 static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
1413 "Result of f(value()) must be a specialization of std::optional");
1414 if (*this)
1415 return std::invoke(std::forward<_Func>(__f), value());
1416 return remove_cvref_t<_Up>();
1417 }
1418
1419 template <class _Func>
1420 requires(is_lvalue_reference_v<_Tp>)
1421 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<remove_cv_t<invoke_result_t<_Func, _Tp&>>>
1422 transform(_Func&& __f) const {
1423 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>;
1424 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
1425 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
1426 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
1427 static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
1428 if (*this)
1429 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
1430 return optional<_Up>();
1431 }
1432
1433 template <invocable _Func>
1434 requires(is_lvalue_reference_v<_Tp>)
1435 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const {
1436 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
1437 "Result of f() should be the same type as this optional");
1438 if (*this)
1439 return *this;
1440 return std::forward<_Func>(__f)();
1441 }
1442# endif
1443};
1444
1445template <class _Tp>
1446optional(_Tp) -> optional<_Tp>;
1447
1448// [optional.relops] Relational operators
1449
1450template <
1451 class _Tp,
1452 class _Up,
1453 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1454 int> = 0>
1455_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, const optional<_Up>& __y) {
1456 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1457 return false;
1458 if (!static_cast<bool>(__x))
1459 return true;
1460 return *__x == *__y;
1461}
1462
1463template <
1464 class _Tp,
1465 class _Up,
1466 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1467 int> = 0>
1468_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) {
1469 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1470 return true;
1471 if (!static_cast<bool>(__x))
1472 return false;
1473 return *__x != *__y;
1474}
1475
1476template < class _Tp,
1477 class _Up,
1478 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1479 int> = 0>
1480_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>& __x, const optional<_Up>& __y) {
1481 if (!static_cast<bool>(__y))
1482 return false;
1483 if (!static_cast<bool>(__x))
1484 return true;
1485 return *__x < *__y;
1486}
1487
1488template < class _Tp,
1489 class _Up,
1490 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1491 int> = 0>
1492_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, const optional<_Up>& __y) {
1493 if (!static_cast<bool>(__x))
1494 return false;
1495 if (!static_cast<bool>(__y))
1496 return true;
1497 return *__x > *__y;
1498}
1499
1500template <
1501 class _Tp,
1502 class _Up,
1503 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1504 int> = 0>
1505_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, const optional<_Up>& __y) {
1506 if (!static_cast<bool>(__x))
1507 return true;
1508 if (!static_cast<bool>(__y))
1509 return false;
1510 return *__x <= *__y;
1511}
1512
1513template <
1514 class _Tp,
1515 class _Up,
1516 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1517 int> = 0>
1518_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) {
1519 if (!static_cast<bool>(__y))
1520 return true;
1521 if (!static_cast<bool>(__x))
1522 return false;
1523 return *__x >= *__y;
1524}
1525
1526# if _LIBCPP_STD_VER >= 20
1527
1528template <class _Tp, three_way_comparable_with<_Tp> _Up>
1529_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
1530operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) {
1531 if (__x && __y)
1532 return *__x <=> *__y;
1533 return __x.has_value() <=> __y.has_value();
1534}
1535
1536# endif // _LIBCPP_STD_VER >= 20
1537
1538// [optional.nullops] Comparison with nullopt
1539
1540template <class _Tp>
1541_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept {
1542 return !static_cast<bool>(__x);
1543}
1544
1545# if _LIBCPP_STD_VER <= 17
1546
1547template <class _Tp>
1548_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(nullopt_t, const optional<_Tp>& __x) noexcept {
1549 return !static_cast<bool>(__x);
1550}
1551
1552template <class _Tp>
1553_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, nullopt_t) noexcept {
1554 return static_cast<bool>(__x);
1555}
1556
1557template <class _Tp>
1558_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(nullopt_t, const optional<_Tp>& __x) noexcept {
1559 return static_cast<bool>(__x);
1560}
1561
1562template <class _Tp>
1563_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>&, nullopt_t) noexcept {
1564 return false;
1565}
1566
1567template <class _Tp>
1568_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(nullopt_t, const optional<_Tp>& __x) noexcept {
1569 return static_cast<bool>(__x);
1570}
1571
1572template <class _Tp>
1573_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, nullopt_t) noexcept {
1574 return !static_cast<bool>(__x);
1575}
1576
1577template <class _Tp>
1578_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(nullopt_t, const optional<_Tp>&) noexcept {
1579 return true;
1580}
1581
1582template <class _Tp>
1583_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, nullopt_t) noexcept {
1584 return static_cast<bool>(__x);
1585}
1586
1587template <class _Tp>
1588_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(nullopt_t, const optional<_Tp>&) noexcept {
1589 return false;
1590}
1591
1592template <class _Tp>
1593_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>&, nullopt_t) noexcept {
1594 return true;
1595}
1596
1597template <class _Tp>
1598_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(nullopt_t, const optional<_Tp>& __x) noexcept {
1599 return !static_cast<bool>(__x);
1600}
1601
1602# else // _LIBCPP_STD_VER <= 17
1603
1604template <class _Tp>
1605_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept {
1606 return __x.has_value() <=> false;
1607}
1608
1609# endif // _LIBCPP_STD_VER <= 17
1610
1611// [optional.comp.with.t] Comparison with T
1612
1613template <
1614 class _Tp,
1615 class _Up,
1616 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1617 int> = 0>
1618_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, const _Up& __v) {
1619 if (__x.has_value())
1620 return *__x == __v;
1621 return false;
1622}
1623
1624template <
1625 class _Tp,
1626 class _Up,
1627 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1628 int> = 0>
1629_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const _Tp& __v, const optional<_Up>& __x) {
1630 if (__x.has_value())
1631 return __v == *__x;
1632 return false;
1633}
1634
1635template <
1636 class _Tp,
1637 class _Up,
1638 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1639 int> = 0>
1640_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, const _Up& __v) {
1641 if (__x.has_value())
1642 return *__x != __v;
1643 return true;
1644}
1645
1646template <
1647 class _Tp,
1648 class _Up,
1649 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1650 int> = 0>
1651_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const _Tp& __v, const optional<_Up>& __x) {
1652 if (__x.has_value())
1653 return __v != *__x;
1654 return true;
1655}
1656
1657template < class _Tp,
1658 class _Up,
1659 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1660 int> = 0>
1661_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>& __x, const _Up& __v) {
1662 if (__x.has_value())
1663 return *__x < __v;
1664 return true;
1665}
1666
1667template < class _Tp,
1668 class _Up,
1669 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1670 int> = 0>
1671_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const _Tp& __v, const optional<_Up>& __x) {
1672 if (__x.has_value())
1673 return __v < *__x;
1674 return false;
1675}
1676
1677template <
1678 class _Tp,
1679 class _Up,
1680 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1681 int> = 0>
1682_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, const _Up& __v) {
1683 if (__x.has_value())
1684 return *__x <= __v;
1685 return true;
1686}
1687
1688template <
1689 class _Tp,
1690 class _Up,
1691 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1692 int> = 0>
1693_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const _Tp& __v, const optional<_Up>& __x) {
1694 if (__x.has_value())
1695 return __v <= *__x;
1696 return false;
1697}
1698
1699template < class _Tp,
1700 class _Up,
1701 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1702 int> = 0>
1703_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, const _Up& __v) {
1704 if (__x.has_value())
1705 return *__x > __v;
1706 return false;
1707}
1708
1709template < class _Tp,
1710 class _Up,
1711 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1712 int> = 0>
1713_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const _Tp& __v, const optional<_Up>& __x) {
1714 if (__x.has_value())
1715 return __v > *__x;
1716 return true;
1717}
1718
1719template <
1720 class _Tp,
1721 class _Up,
1722 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1723 int> = 0>
1724_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>& __x, const _Up& __v) {
1725 if (__x.has_value())
1726 return *__x >= __v;
1727 return false;
1728}
1729
1730template <
1731 class _Tp,
1732 class _Up,
1733 enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1734 int> = 0>
1735_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const _Tp& __v, const optional<_Up>& __x) {
1736 if (__x.has_value())
1737 return __v >= *__x;
1738 return true;
1739}
1740
1741# if _LIBCPP_STD_VER >= 20
1742
1743template <class _Tp, class _Up>
1744 requires(!__is_derived_from_optional<_Up>) && three_way_comparable_with<_Tp, _Up>
1745_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
1746operator<=>(const optional<_Tp>& __x, const _Up& __v) {
1747 return __x.has_value() ? *__x <=> __v : strong_ordering::less;
1748}
1749
1750# endif // _LIBCPP_STD_VER >= 20
1751
1752template <class _Tp, enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, int> = 0>
1753inline _LIBCPP_HIDE_FROM_ABI
1754_LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
1755 __x.swap(__y);
1756}
1757
1758struct __make_optional_barrier_tag {
1759 explicit __make_optional_barrier_tag() = default;
1760};
1761
1762template <
1763# if _LIBCPP_STD_VER >= 26
1764 __make_optional_barrier_tag = __make_optional_barrier_tag{},
1765# endif
1766 class _Tp,
1767 enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>, int> = 0>
1768[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
1769 return optional<decay_t<_Tp>>(std::forward<_Tp>(__v));
1770}
1771
1772template <class _Tp, class... _Args, enable_if_t<__is_constructible_for_optional_v<_Tp, _Args...>, int> = 0>
1773[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
1774 return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
1775}
1776
1777template <class _Tp,
1778 class _Up,
1779 class... _Args,
1780 enable_if_t<__is_constructible_for_optional_initializer_list_v<_Tp, _Up, _Args...>, int> = 0>
1781[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp>
1782make_optional(initializer_list<_Up> __il, _Args&&... __args) {
1783 return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
1784}
1785
1786template <class _Tp>
1787struct hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > {
1788# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1789 _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type;
1790 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
1791# endif
1792
1793 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
1794 return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
1795 }
1796};
1797
1798_LIBCPP_END_NAMESPACE_STD
1799
1800# endif // _LIBCPP_STD_VER >= 17
1801
1802_LIBCPP_POP_MACROS
1803
1804# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1805# include <atomic>
1806# include <climits>
1807# include <concepts>
1808# include <ctime>
1809# include <iterator>
1810# include <limits>
1811# include <memory>
1812# include <ratio>
1813# include <stdexcept>
1814# include <tuple>
1815# include <type_traits>
1816# include <typeinfo>
1817# include <utility>
1818# include <variant>
1819# endif
1820#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1821
1822#endif // _LIBCPP_OPTIONAL