libstdc++
optional
Go to the documentation of this file.
1// <optional> -*- C++ -*-
2
3// Copyright (C) 2013-2026 Free Software Foundation, Inc.
4// Copyright The GNU Toolchain Authors.
5//
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
25
26/** @file include/optional
27 * This is a Standard C++ Library header.
28 */
29
30#ifndef _GLIBCXX_OPTIONAL
31#define _GLIBCXX_OPTIONAL 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#define __glibcxx_want_freestanding_optional
38#define __glibcxx_want_optional
39#define __glibcxx_want_optional_range_support
40#define __glibcxx_want_constrained_equality
41#define __glibcxx_want_constexpr_exceptions
42#include <bits/version.h>
43
44#ifdef __cpp_lib_optional // C++ >= 17
45
46#include <type_traits>
47#include <exception>
48#include <new>
49#include <initializer_list>
53#include <bits/stl_construct.h> // _Construct
54#include <bits/utility.h> // in_place_t
55#if __cplusplus > 201703L
56# include <compare>
57# include <bits/invoke.h> // std::__invoke
58#endif
59#if __cplusplus > 202002L
60# include <concepts>
61#endif
62#ifdef __cpp_lib_optional_range_support // C++ >= 26
63# include <bits/formatfwd.h>
64# include <bits/ranges_base.h>
65# include <bits/stl_iterator.h>
66#endif
67
68namespace std _GLIBCXX_VISIBILITY(default)
69{
70_GLIBCXX_BEGIN_NAMESPACE_VERSION
71
72 /**
73 * @addtogroup utilities
74 * @{
75 */
76
77 template<typename _Tp>
78 class optional;
79
80 /// Tag type to disengage optional objects.
81 struct nullopt_t
82 {
83 // Do not user-declare default constructor at all for
84 // optional_value = {} syntax to work.
85 // nullopt_t() = delete;
86
87 // Used for constructing nullopt.
88 enum class _Construct { _Token };
89
90 // Must be constexpr for nullopt_t to be literal.
91 explicit constexpr nullopt_t(_Construct) noexcept { }
92 };
93
94 /// Tag to disengage optional objects.
95 inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
96
97 template<typename _Fn> struct _Optional_func { _Fn& _M_f; };
98
99 /**
100 * @brief Exception class thrown when a disengaged optional object is
101 * dereferenced.
102 * @ingroup exceptions
103 */
104 class bad_optional_access : public exception
105 {
106 public:
107 bad_optional_access() = default;
108 virtual ~bad_optional_access() = default;
109
110#if __cpp_lib_constexpr_exceptions >= 202502L
111 constexpr
112#endif
113 const char* what() const noexcept override
114 { return "bad optional access"; }
115 };
116
117 // XXX Does not belong here.
118 [[__noreturn__]]
119#if __cpp_lib_constexpr_exceptions >= 202502L
120 constexpr
121#else
122 inline
123#endif
124 void
125 __throw_bad_optional_access()
126 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
127
128 // This class template manages construction/destruction of
129 // the contained value for a std::optional.
130 template <typename _Tp>
131 struct _Optional_payload_base
132 {
133 using _Stored_type = remove_const_t<_Tp>;
134
135 _Optional_payload_base() = default;
136 ~_Optional_payload_base() = default;
137
138 template<typename... _Args>
139 constexpr
140 _Optional_payload_base(in_place_t __tag, _Args&&... __args)
141 : _M_payload(__tag, std::forward<_Args>(__args)...),
142 _M_engaged(true)
143 { }
144
145 template<typename _Up, typename... _Args>
146 constexpr
147 _Optional_payload_base(std::initializer_list<_Up> __il,
148 _Args&&... __args)
149 : _M_payload(__il, std::forward<_Args>(__args)...),
150 _M_engaged(true)
151 { }
152
153 // Constructor used by _Optional_base copy constructor when the
154 // contained value is not trivially copy constructible.
155 constexpr
156 _Optional_payload_base(bool /* __engaged */,
157 const _Optional_payload_base& __other)
158 {
159 if (__other._M_engaged)
160 this->_M_construct(__other._M_get());
161 }
162
163 // Constructor used by _Optional_base move constructor when the
164 // contained value is not trivially move constructible.
165 constexpr
166 _Optional_payload_base(bool /* __engaged */,
167 _Optional_payload_base&& __other)
168 {
169 if (__other._M_engaged)
170 this->_M_construct(std::move(__other._M_get()));
171 }
172
173 // Copy constructor is only used to when the contained value is
174 // trivially copy constructible.
175 _Optional_payload_base(const _Optional_payload_base&) = default;
176
177 // Move constructor is only used to when the contained value is
178 // trivially copy constructible.
179 _Optional_payload_base(_Optional_payload_base&&) = default;
180
181 _Optional_payload_base&
182 operator=(const _Optional_payload_base&) = default;
183
184 _Optional_payload_base&
185 operator=(_Optional_payload_base&&) = default;
186
187 // used to perform non-trivial copy assignment.
188 constexpr void
189 _M_copy_assign(const _Optional_payload_base& __other)
190 {
191 if (this->_M_engaged && __other._M_engaged)
192 this->_M_get() = __other._M_get();
193 else
194 {
195 if (__other._M_engaged)
196 this->_M_construct(__other._M_get());
197 else
198 this->_M_reset();
199 }
200 }
201
202 // used to perform non-trivial move assignment.
203 constexpr void
204 _M_move_assign(_Optional_payload_base&& __other)
205 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
206 is_nothrow_move_assignable<_Tp>>)
207 {
208 if (this->_M_engaged && __other._M_engaged)
209 this->_M_get() = std::move(__other._M_get());
210 else
211 {
212 if (__other._M_engaged)
213 this->_M_construct(std::move(__other._M_get()));
214 else
215 this->_M_reset();
216 }
217 }
218
219 struct _Empty_byte { };
220
221 template<typename _Up, bool = is_trivially_destructible_v<_Up>>
222 union _Storage
223 {
224 constexpr _Storage() noexcept : _M_empty() { }
225
226 template<typename... _Args>
227 constexpr
228 _Storage(in_place_t, _Args&&... __args)
229 : _M_value(std::forward<_Args>(__args)...)
230 { }
231
232 template<typename _Vp, typename... _Args>
233 constexpr
234 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
235 : _M_value(__il, std::forward<_Args>(__args)...)
236 { }
237
238#if __cplusplus >= 202002L
239 template<typename _Fn, typename _Arg>
240 constexpr
241 _Storage(_Optional_func<_Fn> __f, _Arg&& __arg)
242 : _M_value(std::__invoke(std::forward<_Fn>(__f._M_f),
243 std::forward<_Arg>(__arg)))
244 { }
245#endif
246
247#if __cpp_concepts >= 202002L // Conditionally trivial special member functions
248 ~_Storage() = default;
249
250 // User-provided destructor is needed when _Up has non-trivial dtor.
251 _GLIBCXX20_CONSTEXPR
252 ~_Storage() requires (!is_trivially_destructible_v<_Up>)
253 { }
254
255 _Storage(const _Storage&) = default;
256 _Storage(_Storage&&) = default;
257 _Storage& operator=(const _Storage&) = default;
258 _Storage& operator=(_Storage&&) = default;
259#endif
260
261 _Empty_byte _M_empty;
262 _Up _M_value;
263 };
264
265#if __cpp_concepts < 202002L
266 template<typename _Up>
267 union _Storage<_Up, false>
268 {
269 constexpr _Storage() noexcept : _M_empty() { }
270
271 template<typename... _Args>
272 constexpr
273 _Storage(in_place_t, _Args&&... __args)
274 : _M_value(std::forward<_Args>(__args)...)
275 { }
276
277 template<typename _Vp, typename... _Args>
278 constexpr
279 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
280 : _M_value(__il, std::forward<_Args>(__args)...)
281 { }
282
283#if __cplusplus >= 202002L
284 template<typename _Fn, typename _Arg>
285 constexpr
286 _Storage(_Optional_func<_Fn> __f, _Arg&& __arg)
287 : _M_value(std::__invoke(std::forward<_Fn>(__f._M_f),
288 std::forward<_Arg>(__arg)))
289 { }
290#endif
291
292 // User-provided destructor is needed when _Up has non-trivial dtor.
293 _GLIBCXX20_CONSTEXPR ~_Storage() { }
294
295 _Storage(const _Storage&) = default;
296 _Storage(_Storage&&) = default;
297 _Storage& operator=(const _Storage&) = default;
298 _Storage& operator=(_Storage&&) = default;
299
300 _Empty_byte _M_empty;
301 _Up _M_value;
302 };
303#endif
304
305 _Storage<_Stored_type> _M_payload;
306
307 bool _M_engaged = false;
308
309 template<typename... _Args>
310 constexpr void
311 _M_construct(_Args&&... __args)
312 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
313 {
314 std::_Construct(std::__addressof(this->_M_payload._M_value),
315 std::forward<_Args>(__args)...);
316 this->_M_engaged = true;
317 }
318
319 constexpr void
320 _M_destroy() noexcept
321 {
322 _M_engaged = false;
323 _M_payload._M_value.~_Stored_type();
324#if defined(__clang__) && __cpp_lib_optional >= 202106L // full constexpr support
325 if (std::is_constant_evaluated())
326 // Work around PR124910 for Clang.
327 std::construct_at(std::__addressof(_M_payload._M_empty));
328#endif
329 }
330
331#if __cplusplus >= 202002L
332 template<typename _Fn, typename _Up>
333 constexpr void
334 _M_apply(_Optional_func<_Fn> __f, _Up&& __x)
335 {
336 std::construct_at(std::__addressof(this->_M_payload),
337 __f, std::forward<_Up>(__x));
338 _M_engaged = true;
339 }
340#endif
341
342 // The _M_get() operations have _M_engaged as a precondition.
343 // They exist to access the contained value with the appropriate
344 // const-qualification, because _M_payload has had the const removed.
345
346 constexpr _Tp&
347 _M_get() noexcept
348 { return this->_M_payload._M_value; }
349
350 constexpr const _Tp&
351 _M_get() const noexcept
352 { return this->_M_payload._M_value; }
353
354 // _M_reset is a 'safe' operation with no precondition.
355 constexpr void
356 _M_reset() noexcept
357 {
358 if (this->_M_engaged)
359 _M_destroy();
360 else // This seems redundant but improves codegen, see PR 112480.
361 this->_M_engaged = false;
362 }
363 };
364
365 // Class template that manages the payload for optionals.
366 template <typename _Tp,
367 bool /*_HasTrivialDestructor*/ =
368 is_trivially_destructible_v<_Tp>,
369 bool /*_HasTrivialCopy */ =
370 is_trivially_copy_assignable_v<_Tp>
371 && is_trivially_copy_constructible_v<_Tp>,
372 bool /*_HasTrivialMove */ =
373 is_trivially_move_assignable_v<_Tp>
374 && is_trivially_move_constructible_v<_Tp>>
375 struct _Optional_payload;
376
377 // Payload for potentially-constexpr optionals (trivial copy/move/destroy).
378 template <typename _Tp>
379 struct _Optional_payload<_Tp, true, true, true>
380 : _Optional_payload_base<_Tp>
381 {
382 using _Optional_payload_base<_Tp>::_Optional_payload_base;
383
384 _Optional_payload() = default;
385 };
386
387 // Payload for optionals with non-trivial copy construction/assignment.
388 template <typename _Tp>
389 struct _Optional_payload<_Tp, true, false, true>
390 : _Optional_payload_base<_Tp>
391 {
392 using _Optional_payload_base<_Tp>::_Optional_payload_base;
393
394 _Optional_payload() = default;
395 ~_Optional_payload() = default;
396 _Optional_payload(const _Optional_payload&) = default;
397 _Optional_payload(_Optional_payload&&) = default;
398 _Optional_payload& operator=(_Optional_payload&&) = default;
399
400 // Non-trivial copy assignment.
401 constexpr
402 _Optional_payload&
403 operator=(const _Optional_payload& __other)
404 {
405 this->_M_copy_assign(__other);
406 return *this;
407 }
408 };
409
410 // Payload for optionals with non-trivial move construction/assignment.
411 template <typename _Tp>
412 struct _Optional_payload<_Tp, true, true, false>
413 : _Optional_payload_base<_Tp>
414 {
415 using _Optional_payload_base<_Tp>::_Optional_payload_base;
416
417 _Optional_payload() = default;
418 ~_Optional_payload() = default;
419 _Optional_payload(const _Optional_payload&) = default;
420 _Optional_payload(_Optional_payload&&) = default;
421 _Optional_payload& operator=(const _Optional_payload&) = default;
422
423 // Non-trivial move assignment.
424 constexpr
425 _Optional_payload&
426 operator=(_Optional_payload&& __other)
427 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
428 is_nothrow_move_assignable<_Tp>>)
429 {
430 this->_M_move_assign(std::move(__other));
431 return *this;
432 }
433 };
434
435 // Payload for optionals with non-trivial copy and move assignment.
436 template <typename _Tp>
437 struct _Optional_payload<_Tp, true, false, false>
438 : _Optional_payload_base<_Tp>
439 {
440 using _Optional_payload_base<_Tp>::_Optional_payload_base;
441
442 _Optional_payload() = default;
443 ~_Optional_payload() = default;
444 _Optional_payload(const _Optional_payload&) = default;
445 _Optional_payload(_Optional_payload&&) = default;
446
447 // Non-trivial copy assignment.
448 constexpr
449 _Optional_payload&
450 operator=(const _Optional_payload& __other)
451 {
452 this->_M_copy_assign(__other);
453 return *this;
454 }
455
456 // Non-trivial move assignment.
457 constexpr
458 _Optional_payload&
459 operator=(_Optional_payload&& __other)
460 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
461 is_nothrow_move_assignable<_Tp>>)
462 {
463 this->_M_move_assign(std::move(__other));
464 return *this;
465 }
466 };
467
468 // Payload for optionals with non-trivial destructors.
469 template <typename _Tp, bool _Copy, bool _Move>
470 struct _Optional_payload<_Tp, false, _Copy, _Move>
471 : _Optional_payload<_Tp, true, false, false>
472 {
473 // Base class implements all the constructors and assignment operators:
474 using _Optional_payload<_Tp, true, false, false>::_Optional_payload;
475 _Optional_payload() = default;
476 _Optional_payload(const _Optional_payload&) = default;
477 _Optional_payload(_Optional_payload&&) = default;
478 _Optional_payload& operator=(const _Optional_payload&) = default;
479 _Optional_payload& operator=(_Optional_payload&&) = default;
480
481 // Destructor needs to destroy the contained value:
482 _GLIBCXX20_CONSTEXPR ~_Optional_payload() { this->_M_reset(); }
483 };
484
485 /**
486 * @brief Class template that provides copy/move constructors of optional.
487 *
488 * Such a separate base class template is necessary in order to
489 * conditionally make copy/move constructors trivial.
490 *
491 * When the contained value is trivially copy/move constructible,
492 * the copy/move constructors of _Optional_base will invoke the
493 * trivial copy/move constructor of _Optional_payload. Otherwise,
494 * they will invoke _Optional_payload(bool, const _Optional_payload&)
495 * or _Optional_payload(bool, _Optional_payload&&) to initialize
496 * the contained value, if copying/moving an engaged optional.
497 *
498 * Whether the other special members are trivial is determined by the
499 * _Optional_payload<_Tp> specialization used for the _M_payload member.
500 *
501 * @see optional, _Enable_special_members
502 */
503 template<typename _Tp,
504 bool = is_trivially_copy_constructible_v<_Tp>,
505 bool = is_trivially_move_constructible_v<_Tp>>
506 struct _Optional_base
507 {
508 // Constructors for disengaged optionals.
509 constexpr _Optional_base() = default;
510
511 // Constructors for engaged optionals.
512 template<typename... _Args,
513 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
514 constexpr explicit
515 _Optional_base(in_place_t, _Args&&... __args)
516 : _M_payload(in_place, std::forward<_Args>(__args)...)
517 { }
518
519 template<typename _Up, typename... _Args,
520 enable_if_t<is_constructible_v<_Tp,
521 initializer_list<_Up>&,
522 _Args...>, bool> = false>
523 constexpr explicit
524 _Optional_base(in_place_t,
525 initializer_list<_Up> __il,
526 _Args&&... __args)
527 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
528 { }
529
530 // Copy and move constructors.
531 constexpr
532 _Optional_base(const _Optional_base& __other)
533 noexcept(is_nothrow_copy_constructible_v<_Tp>)
534 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
535 { }
536
537 constexpr
538 _Optional_base(_Optional_base&& __other)
539 noexcept(is_nothrow_move_constructible_v<_Tp>)
540 : _M_payload(__other._M_payload._M_engaged,
541 std::move(__other._M_payload))
542 { }
543
544#if __cpp_concepts >= 202002L // Conditionally trivial special member functions
545 // Define these in the primary template if possible, so that we don't
546 // need to use partial specializations of this class template.
547 constexpr _Optional_base(const _Optional_base&)
548 requires is_trivially_copy_constructible_v<_Tp> = default;
549
550 constexpr _Optional_base(_Optional_base&&)
551 requires is_trivially_move_constructible_v<_Tp> = default;
552#endif
553
554 // Assignment operators.
555 _Optional_base& operator=(const _Optional_base&) = default;
556 _Optional_base& operator=(_Optional_base&&) = default;
557
558 _Optional_payload<_Tp> _M_payload;
559
560 protected:
561 // For the primary template, we define these functions here.
562 using _Stored_type = remove_const_t<_Tp>;
563
564 // The _M_construct operation has !_M_engaged as a precondition
565 // while _M_destruct has _M_engaged as a precondition.
566 template<typename... _Args>
567 constexpr void
568 _M_construct(_Args&&... __args)
569 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
570 {
571 _M_payload._M_construct(std::forward<_Args>(__args)...);
572 }
573
574 constexpr void
575 _M_destruct() noexcept
576 { _M_payload._M_destroy(); }
577
578 // _M_reset is a 'safe' operation with no precondition.
579 constexpr void
580 _M_reset() noexcept
581 { _M_payload._M_reset(); }
582
583 constexpr bool _M_is_engaged() const noexcept
584 { return _M_payload._M_engaged; }
585
586 // The _M_get operations have _M_engaged as a precondition.
587 constexpr _Tp&
588 _M_get() noexcept
589 { return _M_payload._M_get(); }
590
591 constexpr const _Tp&
592 _M_get() const noexcept
593 { return _M_payload._M_get(); }
594 };
595
596#if __cpp_concepts < 202002L
597 // If P0848R3 "Conditionally Trivial Special Member Functions" is not
598 // supported (as determined from the __cpp_concepts macro value), the
599 // _Optional_base primary template only has non-trivial copy and move
600 // constructors. Use partial specializations of _Optional_base<T, C, M>
601 // that have a trivial copy and/or move constructor.
602
603 // Common base class for _Optional_base<T> to avoid repeating these
604 // member functions in each partial specialization.
605 // Only used if P0848R3 "Conditionally Trivial Special Member Functions"
606 // is not supported, as indicated by the __cpp_concepts value.
607 template<typename _Tp, typename _Dp>
608 class _Optional_base_impl
609 {
610 protected:
611 using _Stored_type = remove_const_t<_Tp>;
612
613 // The _M_construct operation has !_M_engaged as a precondition
614 // while _M_destruct has _M_engaged as a precondition.
615 template<typename... _Args>
616 constexpr void
617 _M_construct(_Args&&... __args)
618 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
619 {
620 static_cast<_Dp*>(this)->_M_payload._M_construct(
621 std::forward<_Args>(__args)...);
622 }
623
624 constexpr void
625 _M_destruct() noexcept
626 { static_cast<_Dp*>(this)->_M_payload._M_destroy(); }
627
628 // _M_reset is a 'safe' operation with no precondition.
629 constexpr void
630 _M_reset() noexcept
631 { static_cast<_Dp*>(this)->_M_payload._M_reset(); }
632
633 constexpr bool _M_is_engaged() const noexcept
634 { return static_cast<const _Dp*>(this)->_M_payload._M_engaged; }
635
636 // The _M_get operations have _M_engaged as a precondition.
637 constexpr _Tp&
638 _M_get() noexcept
639 { return static_cast<_Dp*>(this)->_M_payload._M_get(); }
640
641 constexpr const _Tp&
642 _M_get() const noexcept
643 { return static_cast<const _Dp*>(this)->_M_payload._M_get(); }
644 };
645
646 template<typename _Tp>
647 struct _Optional_base<_Tp, false, true> // trivial move ctor
648 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
649 {
650 // Constructors for disengaged optionals.
651 constexpr _Optional_base() = default;
652
653 // Constructors for engaged optionals.
654 template<typename... _Args,
655 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
656 constexpr explicit
657 _Optional_base(in_place_t, _Args&&... __args)
658 : _M_payload(in_place, std::forward<_Args>(__args)...)
659 { }
660
661 template<typename _Up, typename... _Args,
662 enable_if_t<is_constructible_v<_Tp,
663 initializer_list<_Up>&,
664 _Args...>, bool> = false>
665 constexpr explicit
666 _Optional_base(in_place_t,
667 initializer_list<_Up> __il,
668 _Args... __args)
669 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
670 { }
671
672 // Copy and move constructors.
673 constexpr _Optional_base(const _Optional_base& __other)
674 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
675 { }
676
677 constexpr _Optional_base(_Optional_base&& __other) = default;
678
679 // Assignment operators.
680 _Optional_base& operator=(const _Optional_base&) = default;
681 _Optional_base& operator=(_Optional_base&&) = default;
682
683 _Optional_payload<_Tp> _M_payload;
684 };
685
686 template<typename _Tp>
687 struct _Optional_base<_Tp, true, false> // trivial copy ctor
688 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
689 {
690 // Constructors for disengaged optionals.
691 constexpr _Optional_base() = default;
692
693 // Constructors for engaged optionals.
694 template<typename... _Args,
695 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
696 constexpr explicit
697 _Optional_base(in_place_t, _Args&&... __args)
698 : _M_payload(in_place, std::forward<_Args>(__args)...)
699 { }
700
701 template<typename _Up, typename... _Args,
702 enable_if_t<is_constructible_v<_Tp,
704 _Args...>, bool> = false>
705 constexpr explicit
706 _Optional_base(in_place_t,
708 _Args&&... __args)
709 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
710 { }
711
712 // Copy and move constructors.
713 constexpr _Optional_base(const _Optional_base& __other) = default;
714
715 constexpr
716 _Optional_base(_Optional_base&& __other)
717 noexcept(is_nothrow_move_constructible_v<_Tp>)
718 : _M_payload(__other._M_payload._M_engaged,
719 std::move(__other._M_payload))
720 { }
721
722 // Assignment operators.
723 _Optional_base& operator=(const _Optional_base&) = default;
724 _Optional_base& operator=(_Optional_base&&) = default;
725
726 _Optional_payload<_Tp> _M_payload;
727 };
728
729 template<typename _Tp>
730 struct _Optional_base<_Tp, true, true> // trivial copy and move ctors
731 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
732 {
733 // Constructors for disengaged optionals.
734 constexpr _Optional_base() = default;
735
736 // Constructors for engaged optionals.
737 template<typename... _Args,
738 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
739 constexpr explicit
740 _Optional_base(in_place_t, _Args&&... __args)
741 : _M_payload(in_place, std::forward<_Args>(__args)...)
742 { }
743
744 template<typename _Up, typename... _Args,
745 enable_if_t<is_constructible_v<_Tp,
747 _Args...>, bool> = false>
748 constexpr explicit
749 _Optional_base(in_place_t,
751 _Args&&... __args)
752 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
753 { }
754
755 // Copy and move constructors.
756 constexpr _Optional_base(const _Optional_base& __other) = default;
757 constexpr _Optional_base(_Optional_base&& __other) = default;
758
759 // Assignment operators.
760 _Optional_base& operator=(const _Optional_base&) = default;
761 _Optional_base& operator=(_Optional_base&&) = default;
762
763 _Optional_payload<_Tp> _M_payload;
764 };
765#endif // __cpp_concepts
766
767 template<typename _Tp>
768 inline constexpr bool __is_optional_v = false;
769 template<typename _Tp>
770 inline constexpr bool __is_optional_v<optional<_Tp>> = true;
771
772 template<typename _Tp, typename _Wp>
773 using __converts_from_any_cvref = __or_<
774 is_constructible<_Tp, _Wp&>, is_convertible<_Wp&, _Tp>,
775 is_constructible<_Tp, _Wp>, is_convertible<_Wp, _Tp>,
776 is_constructible<_Tp, const _Wp&>, is_convertible<const _Wp&, _Tp>,
777 is_constructible<_Tp, const _Wp>, is_convertible<const _Wp, _Tp>
778 >;
779
780 template<typename _Tp, typename _Up>
781 using __converts_from_optional
782 = __converts_from_any_cvref<_Tp, optional<_Up>>;
783
784 template<typename _Tp, typename _Up>
785 using __assigns_from_optional =
786 __or_<is_assignable<_Tp&, const optional<_Up>&>,
790
791#if __cpp_concepts && __cpp_conditional_explicit && __glibcxx_remove_cvref
792# define _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL 1
793#endif
794
795 template<typename _Tp>
796 inline constexpr bool __is_valid_contained_type_for_optional =
797 (
798#if __cpp_lib_optional >= 202506L
799 is_lvalue_reference_v<_Tp> ||
800#endif
801 (is_object_v<_Tp> && is_destructible_v<_Tp> && !is_array_v<_Tp>)
802 )
803 && !is_same_v<remove_cv_t<remove_reference_t<_Tp>>, nullopt_t>
804 && !is_same_v<remove_cv_t<remove_reference_t<_Tp>>, in_place_t>;
805
806 /**
807 * @brief Class template for optional values.
808 */
809 template<typename _Tp>
810 class optional
811 : private _Optional_base<_Tp>,
812 private _Enable_copy_move<
813 // Copy constructor.
814 is_copy_constructible_v<_Tp>,
815 // Copy assignment.
816 __and_v<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>,
817 // Move constructor.
818 is_move_constructible_v<_Tp>,
819 // Move assignment.
820 __and_v<is_move_constructible<_Tp>, is_move_assignable<_Tp>>,
821 // Unique tag type.
822 optional<_Tp>>
823 {
824 static_assert(__is_valid_contained_type_for_optional<_Tp>);
825
826 private:
827 using _Base = _Optional_base<_Tp>;
828
829 // SFINAE helpers
830
831 // _GLIBCXX_RESOLVE_LIB_DEFECTS
832 // 3836. std::expected<bool, E1> conversion constructor
833 // expected(const expected<U, G>&) should take precedence over
834 // expected(U&&) with operator bool
835#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
836 template<typename _From, typename = remove_cv_t<_Tp>>
837 static constexpr bool __not_constructing_bool_from_optional
838 = true;
839
840 // If T is cv bool, remove_cvref_t<U> is not a specialization of optional
841 // i.e. do not initialize a bool from optional<U>::operator bool().
842 template<typename _From>
843 static constexpr bool
844 __not_constructing_bool_from_optional<_From, bool>
845 = !__is_optional_v<remove_cvref_t<_From>>;
846
847 // If T is not cv bool, converts-from-any-cvref<T, optional<U>> is false.
848 // The constructor that converts from optional<U> is disabled if the
849 // contained value can be initialized from optional<U>, so that the
850 // optional(U&&) constructor can be used instead.
851 template<typename _From, typename = remove_cv_t<_Tp>>
852 static constexpr bool __construct_from_contained_value
853 = !__converts_from_optional<_Tp, _From>::value;
854
855 // However, optional<U> can always be converted to bool, so don't apply
856 // this constraint when T is cv bool.
857 template<typename _From>
858 static constexpr bool __construct_from_contained_value<_From, bool>
859 = true;
860#else
861 template<typename _From, typename = remove_cv_t<_Tp>>
862 struct __not_constructing_bool_from_optional
863 : true_type
864 { };
865
866 template<typename _From>
867 struct __not_constructing_bool_from_optional<_From, bool>
868 : bool_constant<!__is_optional_v<__remove_cvref_t<_From>>>
869 { };
870
871 template<typename _From, typename = remove_cv_t<_Tp>>
872 struct __construct_from_contained_value
873 : __not_<__converts_from_optional<_Tp, _From>>
874 { };
875
876 template<typename _From>
877 struct __construct_from_contained_value<_From, bool>
878 : true_type
879 { };
880
881 template<typename _Up>
882 using __not_self = __not_<is_same<optional, __remove_cvref_t<_Up>>>;
883 template<typename _Up>
884 using __not_tag = __not_<is_same<in_place_t, __remove_cvref_t<_Up>>>;
885 template<typename... _Cond>
886 using _Requires = enable_if_t<__and_v<_Cond...>, bool>;
887#endif
888
889 public:
890 using value_type = _Tp;
891#ifdef __cpp_lib_optional_range_support // >= C++26
892 using iterator = __gnu_cxx::__normal_iterator<_Tp*, optional>;
893 using const_iterator = __gnu_cxx::__normal_iterator<const _Tp*, optional>;
894#endif
895
896 constexpr optional() noexcept { }
897
898 constexpr optional(nullopt_t) noexcept { }
899
900 // Converting constructors for engaged optionals.
901#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
902 template<typename _Up = remove_cv_t<_Tp>>
903 requires (!is_same_v<optional, remove_cvref_t<_Up>>)
904 && (!is_same_v<in_place_t, remove_cvref_t<_Up>>)
905 && is_constructible_v<_Tp, _Up>
906 && __not_constructing_bool_from_optional<_Up>
907 constexpr explicit(!is_convertible_v<_Up, _Tp>)
908 optional(_Up&& __t)
909 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
910 : _Base(std::in_place, std::forward<_Up>(__t)) { }
911
912 template<typename _Up>
913 requires (!is_same_v<_Tp, _Up>)
914 && is_constructible_v<_Tp, const _Up&>
915 && __construct_from_contained_value<_Up>
916 constexpr explicit(!is_convertible_v<const _Up&, _Tp>)
917 optional(const optional<_Up>& __t)
918 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
919 {
920 if (__t)
921 emplace(__t._M_fwd());
922 }
923
924 template<typename _Up>
925 requires (!is_same_v<_Tp, _Up>)
926 && is_constructible_v<_Tp, _Up>
927 && __construct_from_contained_value<_Up>
928 constexpr explicit(!is_convertible_v<_Up, _Tp>)
929 optional(optional<_Up>&& __t)
930 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
931 {
932 if (__t)
933 emplace(std::move(__t)._M_fwd());
934 }
935
936 template<typename... _Args>
937 requires is_constructible_v<_Tp, _Args...>
938 explicit constexpr
939 optional(in_place_t, _Args&&... __args)
940 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
941 : _Base(std::in_place, std::forward<_Args>(__args)...)
942 { }
943
944 template<typename _Up, typename... _Args>
945 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
946 explicit constexpr
947 optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
948 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
949 _Args...>)
950 : _Base(std::in_place, __il, std::forward<_Args>(__args)...)
951 { }
952#else
953 template<typename _Up = remove_cv_t<_Tp>,
954 _Requires<__not_self<_Up>, __not_tag<_Up>,
955 is_constructible<_Tp, _Up>,
956 is_convertible<_Up, _Tp>,
957 __not_constructing_bool_from_optional<_Up>> = true>
958 constexpr
959 optional(_Up&& __t)
960 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
961 : _Base(std::in_place, std::forward<_Up>(__t)) { }
962
963 template<typename _Up = remove_cv_t<_Tp>,
964 _Requires<__not_self<_Up>, __not_tag<_Up>,
965 is_constructible<_Tp, _Up>,
966 __not_<is_convertible<_Up, _Tp>>,
967 __not_constructing_bool_from_optional<_Up>> = false>
968 explicit constexpr
969 optional(_Up&& __t)
970 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
971 : _Base(std::in_place, std::forward<_Up>(__t)) { }
972
973 template<typename _Up,
974 _Requires<__not_<is_same<_Tp, _Up>>,
975 is_constructible<_Tp, const _Up&>,
976 is_convertible<const _Up&, _Tp>,
977 __construct_from_contained_value<_Up>> = true>
978 constexpr
979 optional(const optional<_Up>& __t)
980 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
981 {
982 if (__t)
983 emplace(__t._M_fwd());
984 }
985
986 template<typename _Up,
987 _Requires<__not_<is_same<_Tp, _Up>>,
988 is_constructible<_Tp, const _Up&>,
989 __not_<is_convertible<const _Up&, _Tp>>,
990 __construct_from_contained_value<_Up>> = false>
991 explicit constexpr
992 optional(const optional<_Up>& __t)
993 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
994 {
995 if (__t)
996 emplace(__t._M_fwd());
997 }
998
999 template<typename _Up,
1000 _Requires<__not_<is_same<_Tp, _Up>>,
1001 is_constructible<_Tp, _Up>,
1002 is_convertible<_Up, _Tp>,
1003 __construct_from_contained_value<_Up>> = true>
1004 constexpr
1005 optional(optional<_Up>&& __t)
1006 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
1007 {
1008 if (__t)
1009 emplace(std::move(__t)._M_fwd());
1010 }
1011
1012 template<typename _Up,
1013 _Requires<__not_<is_same<_Tp, _Up>>,
1014 is_constructible<_Tp, _Up>,
1015 __not_<is_convertible<_Up, _Tp>>,
1016 __construct_from_contained_value<_Up>> = false>
1017 explicit constexpr
1018 optional(optional<_Up>&& __t)
1019 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
1020 {
1021 if (__t)
1022 emplace(std::move(__t)._M_fwd());
1023 }
1024
1025 template<typename... _Args,
1026 _Requires<is_constructible<_Tp, _Args...>> = false>
1027 explicit constexpr
1028 optional(in_place_t, _Args&&... __args)
1029 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1030 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
1031
1032 template<typename _Up, typename... _Args,
1033 _Requires<is_constructible<_Tp,
1034 initializer_list<_Up>&,
1035 _Args...>> = false>
1036 explicit constexpr
1037 optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
1038 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
1039 _Args...>)
1040 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
1041#endif
1042
1043 // Assignment operators.
1044 _GLIBCXX20_CONSTEXPR optional&
1045 operator=(nullopt_t) noexcept
1046 {
1047 this->_M_reset();
1048 return *this;
1049 }
1050
1051 template<typename _Up = remove_cv_t<_Tp>>
1052#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
1053 requires (!is_same_v<optional, remove_cvref_t<_Up>>)
1054 && (!(is_scalar_v<_Tp> && is_same_v<_Tp, decay_t<_Up>>))
1055 && is_constructible_v<_Tp, _Up>
1056 && is_assignable_v<_Tp&, _Up>
1057 constexpr optional&
1058#else
1060 __not_<__and_<is_scalar<_Tp>,
1061 is_same<_Tp, decay_t<_Up>>>>,
1062 is_constructible<_Tp, _Up>,
1063 is_assignable<_Tp&, _Up>>,
1064 optional&>
1065#endif
1066 operator=(_Up&& __u)
1067 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
1068 is_nothrow_assignable<_Tp&, _Up>>)
1069 {
1070 if (this->_M_is_engaged())
1071 this->_M_get() = std::forward<_Up>(__u);
1072 else
1073 this->_M_construct(std::forward<_Up>(__u));
1074
1075 return *this;
1076 }
1077
1078 template<typename _Up>
1079#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
1080 requires (!is_same_v<_Tp, _Up>)
1081 && is_constructible_v<_Tp, const _Up&>
1082 && is_assignable_v<_Tp&, const _Up&>
1083 && (!__converts_from_optional<_Tp, _Up>::value)
1084 && (!__assigns_from_optional<_Tp, _Up>::value)
1085 constexpr optional&
1086#else
1087 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
1088 is_constructible<_Tp, const _Up&>,
1089 is_assignable<_Tp&, const _Up&>,
1090 __not_<__converts_from_optional<_Tp, _Up>>,
1091 __not_<__assigns_from_optional<_Tp, _Up>>>,
1092 optional&>
1093#endif
1094 operator=(const optional<_Up>& __u)
1095 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
1096 is_nothrow_assignable<_Tp&, const _Up&>>)
1097 {
1098 if (__u)
1099 {
1100 if (this->_M_is_engaged())
1101 this->_M_get() = __u._M_fwd();
1102 else
1103 this->_M_construct(__u._M_fwd());
1104 }
1105 else
1106 {
1107 this->_M_reset();
1108 }
1109 return *this;
1110 }
1111
1112 template<typename _Up>
1113#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
1114 requires (!is_same_v<_Tp, _Up>)
1115 && is_constructible_v<_Tp, _Up>
1116 && is_assignable_v<_Tp&, _Up>
1117 && (!__converts_from_optional<_Tp, _Up>::value)
1118 && (!__assigns_from_optional<_Tp, _Up>::value)
1119 constexpr optional&
1120#else
1121 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
1122 is_constructible<_Tp, _Up>,
1123 is_assignable<_Tp&, _Up>,
1124 __not_<__converts_from_optional<_Tp, _Up>>,
1125 __not_<__assigns_from_optional<_Tp, _Up>>>,
1126 optional&>
1127#endif
1128 operator=(optional<_Up>&& __u)
1129 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
1130 is_nothrow_assignable<_Tp&, _Up>>)
1131 {
1132 if (__u)
1133 {
1134 if (this->_M_is_engaged())
1135 this->_M_get() = std::move(__u)._M_fwd();
1136 else
1137 this->_M_construct(std::move(__u)._M_fwd());
1138 }
1139 else
1140 {
1141 this->_M_reset();
1142 }
1143
1144 return *this;
1145 }
1146
1147 template<typename... _Args>
1148 _GLIBCXX20_CONSTEXPR
1149 enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
1150 emplace(_Args&&... __args)
1151 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1152 {
1153 this->_M_reset();
1154 this->_M_construct(std::forward<_Args>(__args)...);
1155 return this->_M_get();
1156 }
1157
1158 template<typename _Up, typename... _Args>
1159 _GLIBCXX20_CONSTEXPR
1161 _Tp&>
1162 emplace(initializer_list<_Up> __il, _Args&&... __args)
1163 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
1164 _Args...>)
1165 {
1166 this->_M_reset();
1167 this->_M_construct(__il, std::forward<_Args>(__args)...);
1168 return this->_M_get();
1169 }
1170
1171 // Destructor is implicit, implemented in _Optional_base.
1172
1173 // Swap.
1174 _GLIBCXX20_CONSTEXPR void
1175 swap(optional& __other)
1176 noexcept(is_nothrow_move_constructible_v<_Tp>
1177 && is_nothrow_swappable_v<_Tp>)
1178 {
1179 using std::swap;
1180
1181 if (this->_M_is_engaged() && __other._M_is_engaged())
1182 swap(this->_M_get(), __other._M_get());
1183 else if (this->_M_is_engaged())
1184 {
1185 __other._M_construct(std::move(this->_M_get()));
1186 this->_M_destruct();
1187 }
1188 else if (__other._M_is_engaged())
1189 {
1190 this->_M_construct(std::move(__other._M_get()));
1191 __other._M_destruct();
1192 }
1193 }
1194
1195#ifdef __cpp_lib_optional_range_support // >= C++26
1196 // Iterator support.
1197 constexpr iterator begin() noexcept
1198 {
1199 return iterator(
1200 this->_M_is_engaged() ? std::addressof(this->_M_get()) : nullptr
1201 );
1202 }
1203
1204 constexpr const_iterator begin() const noexcept
1205 {
1206 return const_iterator(
1207 this->_M_is_engaged() ? std::addressof(this->_M_get()) : nullptr
1208 );
1209 }
1210
1211 constexpr iterator end() noexcept
1212 {
1213 return begin() + has_value();
1214 }
1215
1216 constexpr const_iterator end() const noexcept
1217 {
1218 return begin() + has_value();
1219 }
1220#endif // __cpp_lib_optional_range_support
1221
1222 // Observers.
1223 constexpr const _Tp*
1224 operator->() const noexcept
1225 {
1226 __glibcxx_assert(this->_M_is_engaged());
1227 return std::__addressof(this->_M_get());
1228 }
1229
1230 constexpr _Tp*
1231 operator->() noexcept
1232 {
1233 __glibcxx_assert(this->_M_is_engaged());
1234 return std::__addressof(this->_M_get());
1235 }
1236
1237 constexpr const _Tp&
1238 operator*() const& noexcept
1239 {
1240 __glibcxx_assert(this->_M_is_engaged());
1241 return this->_M_get();
1242 }
1243
1244 constexpr _Tp&
1245 operator*()& noexcept
1246 {
1247 __glibcxx_assert(this->_M_is_engaged());
1248 return this->_M_get();
1249 }
1250
1251 constexpr _Tp&&
1252 operator*()&& noexcept
1253 {
1254 __glibcxx_assert(this->_M_is_engaged());
1255 return std::move(this->_M_get());
1256 }
1257
1258 constexpr const _Tp&&
1259 operator*() const&& noexcept
1260 {
1261 __glibcxx_assert(this->_M_is_engaged());
1262 return std::move(this->_M_get());
1263 }
1264
1265 constexpr explicit operator bool() const noexcept
1266 { return this->_M_is_engaged(); }
1267
1268 constexpr bool has_value() const noexcept
1269 { return this->_M_is_engaged(); }
1270
1271 constexpr const _Tp&
1272 value() const&
1273 {
1274 if (this->_M_is_engaged())
1275 return this->_M_get();
1276 __throw_bad_optional_access();
1277 }
1278
1279 constexpr _Tp&
1280 value()&
1281 {
1282 if (this->_M_is_engaged())
1283 return this->_M_get();
1284 __throw_bad_optional_access();
1285 }
1286
1287 constexpr _Tp&&
1288 value()&&
1289 {
1290 if (this->_M_is_engaged())
1291 return std::move(this->_M_get());
1292 __throw_bad_optional_access();
1293 }
1294
1295 constexpr const _Tp&&
1296 value() const&&
1297 {
1298 if (this->_M_is_engaged())
1299 return std::move(this->_M_get());
1300 __throw_bad_optional_access();
1301 }
1302
1303 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1304 // 4406. value_or return statement is inconsistent with Mandates
1305 template<typename _Up = remove_cv_t<_Tp>>
1306 constexpr remove_cv_t<_Tp>
1307 value_or(_Up&& __u) const&
1308 {
1309 using _Xp = remove_cv_t<_Tp>;
1310 static_assert(is_convertible_v<const _Tp&, _Xp>);
1311 static_assert(is_convertible_v<_Up, _Xp>);
1312
1313 if (this->_M_is_engaged())
1314 return this->_M_get();
1315 return std::forward<_Up>(__u);
1316 }
1317
1318 template<typename _Up = remove_cv_t<_Tp>>
1319 constexpr remove_cv_t<_Tp>
1320 value_or(_Up&& __u) &&
1321 {
1322 using _Xp = remove_cv_t<_Tp>;
1323 static_assert(is_convertible_v<_Tp, _Xp>);
1324 static_assert(is_convertible_v<_Up, _Xp>);
1325
1326 if (this->_M_is_engaged())
1327 return std::move(this->_M_get());
1328 return std::forward<_Up>(__u);
1329 }
1330
1331#if __cpp_lib_optional >= 202110L // C++23
1332 // [optional.monadic]
1333
1334 template<typename _Fn>
1335 constexpr auto
1336 and_then(_Fn&& __f) &
1337 {
1338 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp&>>;
1339 static_assert(__is_optional_v<_Up>,
1340 "the function passed to std::optional<T>::and_then "
1341 "must return a std::optional");
1342 if (has_value())
1343 return std::__invoke(std::forward<_Fn>(__f), _M_get());
1344 else
1345 return _Up();
1346 }
1347
1348 template<typename _Fn>
1349 constexpr auto
1350 and_then(_Fn&& __f) const &
1351 {
1352 using _Up = remove_cvref_t<invoke_result_t<_Fn, const _Tp&>>;
1353 static_assert(__is_optional_v<_Up>,
1354 "the function passed to std::optional<T>::and_then "
1355 "must return a std::optional");
1356 if (has_value())
1357 return std::__invoke(std::forward<_Fn>(__f), _M_get());
1358 else
1359 return _Up();
1360 }
1361
1362 template<typename _Fn>
1363 constexpr auto
1364 and_then(_Fn&& __f) &&
1365 {
1366 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp>>;
1367 static_assert(__is_optional_v<_Up>,
1368 "the function passed to std::optional<T>::and_then "
1369 "must return a std::optional");
1370 if (has_value())
1371 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_get()));
1372 else
1373 return _Up();
1374 }
1375
1376 template<typename _Fn>
1377 constexpr auto
1378 and_then(_Fn&& __f) const &&
1379 {
1380 using _Up = remove_cvref_t<invoke_result_t<_Fn, const _Tp>>;
1381 static_assert(__is_optional_v<_Up>,
1382 "the function passed to std::optional<T>::and_then "
1383 "must return a std::optional");
1384 if (has_value())
1385 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_get()));
1386 else
1387 return _Up();
1388 }
1389
1390 template<typename _Fn>
1391 constexpr auto
1392 transform(_Fn&& __f) &
1393 {
1394 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp&>>;
1395 if (has_value())
1396 return optional<_Up>(_Optional_func<_Fn>{__f}, _M_get());
1397 else
1398 return optional<_Up>();
1399 }
1400
1401 template<typename _Fn>
1402 constexpr auto
1403 transform(_Fn&& __f) const &
1404 {
1405 using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp&>>;
1406 if (has_value())
1407 return optional<_Up>(_Optional_func<_Fn>{__f}, _M_get());
1408 else
1409 return optional<_Up>();
1410 }
1411
1412 template<typename _Fn>
1413 constexpr auto
1414 transform(_Fn&& __f) &&
1415 {
1416 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp>>;
1417 if (has_value())
1418 return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(_M_get()));
1419 else
1420 return optional<_Up>();
1421 }
1422
1423 template<typename _Fn>
1424 constexpr auto
1425 transform(_Fn&& __f) const &&
1426 {
1427 using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp>>;
1428 if (has_value())
1429 return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(_M_get()));
1430 else
1431 return optional<_Up>();
1432 }
1433
1434 template<typename _Fn> requires invocable<_Fn> && copy_constructible<_Tp>
1435 constexpr optional
1436 or_else(_Fn&& __f) const&
1437 {
1438 using _Up = invoke_result_t<_Fn>;
1439 static_assert(is_same_v<remove_cvref_t<_Up>, optional>,
1440 "the function passed to std::optional<T>::or_else "
1441 "must return a std::optional<T>");
1442
1443 if (has_value())
1444 return *this;
1445 else
1446 return std::forward<_Fn>(__f)();
1447 }
1448
1449 template<typename _Fn> requires invocable<_Fn> && move_constructible<_Tp>
1450 constexpr optional
1451 or_else(_Fn&& __f) &&
1452 {
1453 using _Up = invoke_result_t<_Fn>;
1454 static_assert(is_same_v<remove_cvref_t<_Up>, optional>,
1455 "the function passed to std::optional<T>::or_else "
1456 "must return a std::optional<T>");
1457
1458 if (has_value())
1459 return std::move(*this);
1460 else
1461 return std::forward<_Fn>(__f)();
1462 }
1463#endif
1464
1465 _GLIBCXX20_CONSTEXPR void reset() noexcept { this->_M_reset(); }
1466
1467 private:
1468 using _Base::_M_get;
1469
1470 [[__gnu__::__always_inline__]]
1471 constexpr _Tp&
1472 _M_fwd() & noexcept
1473 { return _M_get(); }
1474
1475 [[__gnu__::__always_inline__]]
1476 constexpr _Tp&&
1477 _M_fwd() && noexcept
1478 { return std::move(_M_get()); }
1479
1480 [[__gnu__::__always_inline__]]
1481 constexpr const _Tp&
1482 _M_fwd() const& noexcept
1483 { return _M_get(); }
1484
1485 [[__gnu__::__always_inline__]]
1486 constexpr const _Tp&&
1487 _M_fwd() const&& noexcept
1488 { return std::move(_M_get()); }
1489
1490 template<typename _Up> friend class optional;
1491
1492#if __cpp_lib_optional >= 202110L // C++23
1493 template<typename _Fn, typename _Value>
1494 explicit constexpr
1495 optional(_Optional_func<_Fn> __f, _Value&& __v)
1496 {
1497 this->_M_payload._M_apply(__f, std::forward<_Value>(__v));
1498 }
1499#endif
1500 };
1501
1502#if __cpp_lib_optional >= 202506L // C++26
1503 template<typename _Tp>
1504 class optional<_Tp&>;
1505
1506 template<typename _Tp>
1507 constexpr bool __is_optional_ref_v = false;
1508
1509 template<typename _Tp>
1510 constexpr bool __is_optional_ref_v<optional<_Tp&>> = true;
1511
1512 template<typename _Tp>
1513 struct __optional_ref_base
1514 {};
1515
1516#ifdef __cpp_lib_optional_range_support // >= C++26
1517 template<typename _Tp>
1518 struct __optional_ref_base<_Tp[]>
1519 {};
1520
1521 template<typename _Tp>
1522 requires is_object_v<_Tp>
1523 struct __optional_ref_base<_Tp>
1524 {
1525 using iterator = __gnu_cxx::__normal_iterator<_Tp*, optional<_Tp&>>;
1526 };
1527#endif // __cpp_lib_optional_range_support
1528
1529 template<typename _Tp>
1530 class optional<_Tp&> : public __optional_ref_base<_Tp>
1531 {
1532 static_assert(__is_valid_contained_type_for_optional<_Tp&>);
1533
1534 public:
1535 using value_type = _Tp;
1536
1537 // Constructors.
1538 constexpr optional() noexcept = default;
1539 constexpr optional(nullopt_t) noexcept : optional() {}
1540 constexpr optional(const optional&) noexcept = default;
1541
1542 template<typename _Arg>
1543 requires is_constructible_v<_Tp&, _Arg>
1544 && (!reference_constructs_from_temporary_v<_Tp&, _Arg>)
1545 explicit constexpr
1546 optional(in_place_t, _Arg&& __arg)
1547 {
1548 __convert_ref_init_val(std::forward<_Arg>(__arg));
1549 }
1550
1551 template<typename _Up>
1552 requires (!is_same_v<remove_cvref_t<_Up>, optional>)
1553 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
1554 && is_constructible_v<_Tp&, _Up>
1555 && (!reference_constructs_from_temporary_v<_Tp&, _Up>)
1556 explicit(!is_convertible_v<_Up, _Tp&>)
1557 constexpr
1558 optional(_Up&& __u)
1559 noexcept(is_nothrow_constructible_v<_Tp&, _Up>)
1560 {
1561 __convert_ref_init_val(std::forward<_Up>(__u));
1562 }
1563
1564 template<typename _Up>
1565 requires (!is_same_v<remove_cvref_t<_Up>, optional>)
1566 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
1567 && is_constructible_v<_Tp&, _Up>
1568 && reference_constructs_from_temporary_v<_Tp&, _Up>
1569 explicit(!is_convertible_v<_Up, _Tp&>)
1570 constexpr
1571 optional(_Up&& __u) = delete;
1572
1573 // optional<U> &
1574 template<typename _Up>
1575 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1576 && (!is_same_v<_Tp&, _Up>)
1577 && is_constructible_v<_Tp&, _Up&>
1578 && (!reference_constructs_from_temporary_v<_Tp&, _Up&>)
1579 explicit(!is_convertible_v<_Up&, _Tp&>)
1580 constexpr
1581 optional(optional<_Up>& __rhs)
1582 noexcept(is_nothrow_constructible_v<_Tp&, _Up&>)
1583 {
1584 if (__rhs)
1585 __convert_ref_init_val(__rhs._M_fwd());
1586 }
1587
1588 template<typename _Up>
1589 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1590 && (!is_same_v<_Tp&, _Up>)
1591 && is_constructible_v<_Tp&, _Up&>
1592 && reference_constructs_from_temporary_v<_Tp&, _Up&>
1593 explicit(!is_convertible_v<_Up&, _Tp&>)
1594 constexpr
1595 optional(optional<_Up>& __rhs) = delete;
1596
1597 // const optional<U>&
1598 template<typename _Up>
1599 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1600 && (!is_same_v<_Tp&, _Up>)
1601 && is_constructible_v<_Tp&, const _Up&>
1602 && (!reference_constructs_from_temporary_v<_Tp&, const _Up&>)
1603 explicit(!is_convertible_v<const _Up&, _Tp&>)
1604 constexpr
1605 optional(const optional<_Up>& __rhs)
1606 noexcept(is_nothrow_constructible_v<_Tp&, _Up&>)
1607 {
1608 if (__rhs)
1609 __convert_ref_init_val(__rhs._M_fwd());
1610 }
1611
1612 template<typename _Up>
1613 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1614 && (!is_same_v<_Tp&, _Up>)
1615 && is_constructible_v<_Tp&, const _Up&>
1616 && reference_constructs_from_temporary_v<_Tp&, const _Up&>
1617 explicit(!is_convertible_v<const _Up&, _Tp&>)
1618 constexpr
1619 optional(const optional<_Up>& __rhs) = delete;
1620
1621 // optional<U>&&
1622 template<typename _Up>
1623 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1624 && (!is_same_v<_Tp&, _Up>)
1625 && is_constructible_v<_Tp&, _Up>
1626 && (!reference_constructs_from_temporary_v<_Tp&, _Up>)
1627 explicit(!is_convertible_v<_Up, _Tp&>)
1628 constexpr
1629 optional(optional<_Up>&& __rhs)
1630 noexcept(is_nothrow_constructible_v<_Tp&, _Up>)
1631 {
1632 if (__rhs)
1633 __convert_ref_init_val(std::move(__rhs)._M_fwd());
1634 }
1635
1636 template<typename _Up>
1637 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1638 && (!is_same_v<_Tp&, _Up>)
1639 && is_constructible_v<_Tp&, _Up>
1640 && reference_constructs_from_temporary_v<_Tp&, _Up>
1641 explicit(!is_convertible_v<_Up, _Tp&>)
1642 constexpr
1643 optional(optional<_Up>&& __rhs) = delete;
1644
1645 // const optional<U>&&
1646 template<typename _Up>
1647 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1648 && (!is_same_v<_Tp&, _Up>)
1649 && is_constructible_v<_Tp&, const _Up>
1650 && (!reference_constructs_from_temporary_v<_Tp&, _Up>)
1651 explicit(!is_convertible_v<const _Up, _Tp&>)
1652 constexpr
1653 optional(const optional<_Up>&& __rhs)
1654 noexcept(is_nothrow_constructible_v<_Tp&, const _Up>)
1655 {
1656 if (__rhs)
1657 __convert_ref_init_val(std::move(__rhs)._M_fwd());
1658 }
1659
1660 template<typename _Up>
1661 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1662 && (!is_same_v<_Tp&, _Up>)
1663 && is_constructible_v<_Tp&, const _Up>
1664 && reference_constructs_from_temporary_v<_Tp&, const _Up>
1665 explicit(!is_convertible_v<const _Up, _Tp&>)
1666 constexpr
1667 optional(const optional<_Up>&& __rhs) = delete;
1668
1669 constexpr ~optional() = default;
1670
1671 // Assignment.
1672 constexpr optional& operator=(nullopt_t) noexcept
1673 {
1674 _M_val = nullptr;
1675 return *this;
1676 }
1677
1678 constexpr optional& operator=(const optional&) noexcept = default;
1679
1680 template<typename _Up>
1681 requires is_constructible_v<_Tp&, _Up>
1682 && (!reference_constructs_from_temporary_v<_Tp&, _Up>)
1683 constexpr _Tp&
1684 emplace(_Up&& __u)
1685 noexcept(is_nothrow_constructible_v<_Tp&, _Up>)
1686 {
1687 __convert_ref_init_val(std::forward<_Up>(__u));
1688 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1689 // 4300. Missing Returns: element in optional<T&>::emplace
1690 return *_M_val;
1691 }
1692
1693 // Swap.
1694 constexpr void swap(optional& __rhs) noexcept
1695 { std::swap(_M_val, __rhs._M_val); }
1696
1697#ifdef __cpp_lib_optional_range_support // >= C++26
1698 // Iterator support.
1699 constexpr auto begin() const noexcept
1700 requires is_object_v<_Tp> && (!is_unbounded_array_v<_Tp>)
1701 { return __gnu_cxx::__normal_iterator<_Tp*, optional>(_M_val); }
1702
1703 constexpr auto end() const noexcept
1704 requires is_object_v<_Tp> && (!is_unbounded_array_v<_Tp>)
1705 { return begin() + has_value(); }
1706#endif // __cpp_lib_optional_range_support
1707
1708 // Observers.
1709 constexpr _Tp* operator->() const noexcept
1710 {
1711 __glibcxx_assert(_M_val); // hardened precondition
1712 return _M_val;
1713 }
1714
1715 constexpr _Tp& operator*() const noexcept
1716 {
1717 __glibcxx_assert(_M_val); // hardened precondition
1718 return *_M_val;
1719 }
1720
1721 constexpr explicit operator bool() const noexcept
1722 {
1723 return _M_val;
1724 }
1725
1726 constexpr bool has_value() const noexcept
1727 {
1728 return _M_val;
1729 }
1730
1731 constexpr _Tp& value() const
1732 {
1733 if (_M_val)
1734 return *_M_val;
1735 __throw_bad_optional_access();
1736 }
1737
1738 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1739 // 4304. std::optional<NonReturnable&> is ill-formed due to value_or
1740 template<typename _Up = remove_cv_t<_Tp>>
1741 requires is_object_v<_Tp> && (!is_array_v<_Tp>)
1742 constexpr decay_t<_Tp>
1743 value_or(_Up&& __u) const
1744 {
1745 using _Xp = remove_cv_t<_Tp>;
1746 static_assert(is_convertible_v<_Tp&, _Xp>);
1747 static_assert(is_convertible_v<_Up, _Xp>);
1748 if (_M_val)
1749 return *_M_val;
1750 return std::forward<_Up>(__u);
1751 }
1752
1753 // Monadic operations.
1754 template<typename _Fn>
1755 constexpr auto
1756 and_then(_Fn&& __f) const
1757 {
1758 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp&>>;
1759 static_assert(__is_optional_v<_Up>,
1760 "the function passed to std::optional<T&>::and_then "
1761 "must return a std::optional");
1762 if (has_value())
1763 return std::__invoke(std::forward<_Fn>(__f), *_M_val);
1764 else
1765 return _Up();
1766 }
1767
1768 template<typename _Fn>
1769 constexpr
1770 optional<remove_cv_t<invoke_result_t<_Fn, _Tp&>>>
1771 transform(_Fn&& __f) const
1772 {
1773 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp&>>;
1774 if (has_value())
1775 return optional<_Up>(_Optional_func<_Fn>{__f}, *_M_val);
1776 else
1777 return optional<_Up>();
1778 }
1779
1780 template<typename _Fn>
1781 requires invocable<_Fn>
1782 constexpr
1783 optional
1784 or_else(_Fn&& __f) const
1785 {
1786 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Fn>>, optional>,
1787 "the function passed to std::optional<T&>::or_else "
1788 "must return a std::optional<T&>");
1789 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1790 // 4367. Improve optional<T&>::or_else
1791 if (has_value())
1792 return *this;
1793 else
1794 return std::forward<_Fn>(__f)();
1795 }
1796
1797 // Modifiers.
1798 constexpr void reset() noexcept
1799 {
1800 _M_val = nullptr;
1801 }
1802
1803 private:
1804 _Tp *_M_val = nullptr;
1805
1806 [[__gnu__::__always_inline__]]
1807 constexpr _Tp&
1808 _M_fwd() const noexcept
1809 { return *_M_val; }
1810
1811 template<typename _Up> friend class optional;
1812
1813 template<typename _Up>
1814 constexpr
1815 void
1816 __convert_ref_init_val(_Up&& __u)
1817 noexcept
1818 {
1819 _Tp& __r(std::forward<_Up>(__u));
1820 _M_val = std::addressof(__r);
1821 }
1822
1823 template<typename _Fn, typename _Value>
1824 explicit constexpr
1825 optional(_Optional_func<_Fn> __f, _Value&& __v)
1826 {
1827 _Tp& __r = std::__invoke(std::forward<_Fn>(__f._M_f), std::forward<_Value>(__v));
1828 _M_val = std::addressof(__r);
1829 }
1830 };
1831#endif // __cpp_lib_optional >= 202506L
1832
1833 template<typename _Tp>
1834 using __optional_relop_t =
1836
1837 template<typename _Tp, typename _Up>
1838 using __optional_eq_t = __optional_relop_t<
1840 >;
1841
1842 template<typename _Tp, typename _Up>
1843 using __optional_ne_t = __optional_relop_t<
1845 >;
1846
1847 template<typename _Tp, typename _Up>
1848 using __optional_lt_t = __optional_relop_t<
1850 >;
1851
1852 template<typename _Tp, typename _Up>
1853 using __optional_gt_t = __optional_relop_t<
1855 >;
1856
1857 template<typename _Tp, typename _Up>
1858 using __optional_le_t = __optional_relop_t<
1860 >;
1861
1862 template<typename _Tp, typename _Up>
1863 using __optional_ge_t = __optional_relop_t<
1865 >;
1866
1867 // Comparisons between optional values.
1868 template<typename _Tp, typename _Up>
1869 constexpr auto
1870 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1871 -> __optional_eq_t<_Tp, _Up>
1872 {
1873 if (__lhs.has_value() != __rhs.has_value())
1874 return false;
1875 if (!__lhs.has_value())
1876 return true;
1877 return *__lhs == *__rhs;
1878 }
1879
1880 template<typename _Tp, typename _Up>
1881 constexpr auto
1882 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1883 -> __optional_ne_t<_Tp, _Up>
1884 {
1885 if (__lhs.has_value() != __rhs.has_value())
1886 return true;
1887 if (!__lhs.has_value())
1888 return false;
1889 return *__lhs != *__rhs;
1890 }
1891
1892 template<typename _Tp, typename _Up>
1893 constexpr auto
1894 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1895 -> __optional_lt_t<_Tp, _Up>
1896 {
1897 if (!__rhs.has_value())
1898 return false;
1899 if (!__lhs.has_value())
1900 return true;
1901 return *__lhs < *__rhs;
1902 }
1903
1904 template<typename _Tp, typename _Up>
1905 constexpr auto
1906 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1907 -> __optional_gt_t<_Tp, _Up>
1908 {
1909 if (!__lhs.has_value())
1910 return false;
1911 if (!__rhs.has_value())
1912 return true;
1913 return *__lhs > *__rhs;
1914 }
1915
1916 template<typename _Tp, typename _Up>
1917 constexpr auto
1918 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1919 -> __optional_le_t<_Tp, _Up>
1920 {
1921 if (!__lhs.has_value())
1922 return true;
1923 if (!__rhs.has_value())
1924 return false;
1925 return *__lhs <= *__rhs;
1926 }
1927
1928 template<typename _Tp, typename _Up>
1929 constexpr auto
1930 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1931 -> __optional_ge_t<_Tp, _Up>
1932 {
1933 if (!__rhs.has_value())
1934 return true;
1935 if (!__lhs.has_value())
1936 return false;
1937 return *__lhs >= *__rhs;
1938 }
1939
1940#ifdef __cpp_lib_three_way_comparison
1941 template<typename _Tp, three_way_comparable_with<_Tp> _Up>
1942 [[nodiscard]]
1944 operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
1945 {
1946 return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
1947 }
1948#endif
1949
1950 // Comparisons with nullopt.
1951 template<typename _Tp>
1952 [[nodiscard]]
1953 constexpr bool
1954 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
1955 { return !__lhs; }
1956
1957#ifdef __cpp_lib_three_way_comparison
1958 template<typename _Tp>
1959 [[nodiscard]]
1960 constexpr strong_ordering
1961 operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
1962 { return bool(__x) <=> false; }
1963#else
1964 template<typename _Tp>
1965 constexpr bool
1966 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
1967 { return !__rhs; }
1968
1969 template<typename _Tp>
1970 constexpr bool
1971 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1972 { return static_cast<bool>(__lhs); }
1973
1974 template<typename _Tp>
1975 constexpr bool
1976 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1977 { return static_cast<bool>(__rhs); }
1978
1979 template<typename _Tp>
1980 constexpr bool
1981 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1982 { return false; }
1983
1984 template<typename _Tp>
1985 constexpr bool
1986 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
1987 { return static_cast<bool>(__rhs); }
1988
1989 template<typename _Tp>
1990 constexpr bool
1991 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
1992 { return static_cast<bool>(__lhs); }
1993
1994 template<typename _Tp>
1995 constexpr bool
1996 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1997 { return false; }
1998
1999 template<typename _Tp>
2000 constexpr bool
2001 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
2002 { return !__lhs; }
2003
2004 template<typename _Tp>
2005 constexpr bool
2006 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
2007 { return true; }
2008
2009 template<typename _Tp>
2010 constexpr bool
2011 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
2012 { return true; }
2013
2014 template<typename _Tp>
2015 constexpr bool
2016 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
2017 { return !__rhs; }
2018#endif // three-way-comparison
2019
2020#if __cpp_lib_concepts
2021 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2022 // 4072. std::optional comparisons: constrain harder
2023# define _REQUIRES_NOT_OPTIONAL(T) requires (!__is_optional_v<T>)
2024#else
2025# define _REQUIRES_NOT_OPTIONAL(T)
2026#endif
2027
2028 // Comparisons with value type.
2029 template<typename _Tp, typename _Up>
2030 _REQUIRES_NOT_OPTIONAL(_Up)
2031 constexpr auto
2032 operator== [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2033 -> __optional_eq_t<_Tp, _Up>
2034 {
2035 if (__lhs.has_value())
2036 return *__lhs == __rhs;
2037 return false;
2038 }
2039
2040 template<typename _Tp, typename _Up>
2041 _REQUIRES_NOT_OPTIONAL(_Tp)
2042 constexpr auto
2043 operator== [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2044 -> __optional_eq_t<_Tp, _Up>
2045 {
2046 if (__rhs.has_value())
2047 return __lhs == *__rhs;
2048 return false;
2049 }
2050
2051 template<typename _Tp, typename _Up>
2052 _REQUIRES_NOT_OPTIONAL(_Up)
2053 constexpr auto
2054 operator!= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2055 -> __optional_ne_t<_Tp, _Up>
2056 {
2057 if (__lhs.has_value())
2058 return *__lhs != __rhs;
2059 return true;
2060 }
2061
2062 template<typename _Tp, typename _Up>
2063 _REQUIRES_NOT_OPTIONAL(_Tp)
2064 constexpr auto
2065 operator!= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2066 -> __optional_ne_t<_Tp, _Up>
2067 {
2068 if (__rhs.has_value())
2069 return __lhs != *__rhs;
2070 return true;
2071 }
2072
2073 template<typename _Tp, typename _Up>
2074 _REQUIRES_NOT_OPTIONAL(_Up)
2075 constexpr auto
2076 operator< [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2077 -> __optional_lt_t<_Tp, _Up>
2078 {
2079 if (__lhs.has_value())
2080 return *__lhs < __rhs;
2081 return true;
2082 }
2083
2084 template<typename _Tp, typename _Up>
2085 _REQUIRES_NOT_OPTIONAL(_Tp)
2086 constexpr auto
2087 operator< [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2088 -> __optional_lt_t<_Tp, _Up>
2089 {
2090 if (__rhs.has_value())
2091 return __lhs < *__rhs;
2092 return false;
2093 }
2094
2095 template<typename _Tp, typename _Up>
2096 _REQUIRES_NOT_OPTIONAL(_Up)
2097 constexpr auto
2098 operator> [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2099 -> __optional_gt_t<_Tp, _Up>
2100 {
2101 if (__lhs.has_value())
2102 return *__lhs > __rhs;
2103 return false;
2104 }
2105
2106 template<typename _Tp, typename _Up>
2107 _REQUIRES_NOT_OPTIONAL(_Tp)
2108 constexpr auto
2109 operator> [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2110 -> __optional_gt_t<_Tp, _Up>
2111 {
2112 if (__rhs.has_value())
2113 return __lhs > *__rhs;
2114 return true;
2115 }
2116
2117 template<typename _Tp, typename _Up>
2118 _REQUIRES_NOT_OPTIONAL(_Up)
2119 constexpr auto
2120 operator<= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2121 -> __optional_le_t<_Tp, _Up>
2122 {
2123 if (__lhs.has_value())
2124 return *__lhs <= __rhs;
2125 return true;
2126 }
2127
2128 template<typename _Tp, typename _Up>
2129 _REQUIRES_NOT_OPTIONAL(_Tp)
2130 constexpr auto
2131 operator<= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2132 -> __optional_le_t<_Tp, _Up>
2133 {
2134 if (__rhs.has_value())
2135 return __lhs <= *__rhs;
2136 return false;
2137 }
2138
2139 template<typename _Tp, typename _Up>
2140 _REQUIRES_NOT_OPTIONAL(_Up)
2141 constexpr auto
2142 operator>= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2143 -> __optional_ge_t<_Tp, _Up>
2144 {
2145 if (__lhs.has_value())
2146 return *__lhs >= __rhs;
2147 return false;
2148 }
2149
2150 template<typename _Tp, typename _Up>
2151 _REQUIRES_NOT_OPTIONAL(_Tp)
2152 constexpr auto
2153 operator>= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2154 -> __optional_ge_t<_Tp, _Up>
2155 {
2156 if (__rhs.has_value())
2157 return __lhs >= *__rhs;
2158 return true;
2159 }
2160
2161#ifdef __cpp_lib_three_way_comparison
2162 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2163 // 3746. optional's spaceship with U with a type derived from optional
2164 // causes infinite constraint meta-recursion
2165 template<typename _Tp>
2166 concept __is_derived_from_optional = requires (const _Tp& __t) {
2167 []<typename _Up>(const optional<_Up>&){ }(__t);
2168 };
2169
2170 template<typename _Tp, typename _Up>
2171 requires (!__is_derived_from_optional<_Up>)
2172 && requires { typename compare_three_way_result_t<_Tp, _Up>; }
2173 && three_way_comparable_with<_Tp, _Up>
2175 operator<=> [[nodiscard]] (const optional<_Tp>& __x, const _Up& __v)
2176 { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
2177#endif
2178
2179 // Swap and creation functions.
2180
2181 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2182 // 2748. swappable traits for optionals
2183 template<typename _Tp>
2184 _GLIBCXX20_CONSTEXPR
2185 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
2186 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
2187 noexcept(noexcept(__lhs.swap(__rhs)))
2188 { __lhs.swap(__rhs); }
2189
2190#if __cpp_lib_optional >= 202506L
2191 // We deviate from standard, that do not declared separate swap overload
2192 // from optional<T&>.
2193 template<typename _Tp>
2194 constexpr void
2195 swap(optional<_Tp&>& __lhs, optional<_Tp&>& __rhs) noexcept
2196 { __lhs.swap(__rhs); }
2197#endif
2198
2199 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2200 // 2766. Swapping non-swappable types
2201 template<typename _Tp>
2202 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
2203 swap(optional<_Tp>&, optional<_Tp>&) = delete;
2204
2205#if __cpp_lib_optional >= 202506L
2206 template<int = 0, typename _Tp>
2207#else
2208 template<typename _Tp>
2209#endif
2210 constexpr
2212 optional<decay_t<_Tp>>>
2213 make_optional(_Tp&& __t)
2214 noexcept(is_nothrow_constructible_v<optional<decay_t<_Tp>>, _Tp>)
2215 { return optional<decay_t<_Tp>>( std::forward<_Tp>(__t) ); }
2216
2217 template<typename _Tp, typename... _Args>
2218 constexpr
2219 enable_if_t<is_constructible_v<_Tp, _Args...>,
2220 optional<_Tp>>
2221 make_optional(_Args&&... __args)
2222 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
2223 { return optional<_Tp>( in_place, std::forward<_Args>(__args)... ); }
2224
2225 template<typename _Tp, typename _Up, typename... _Args>
2226 constexpr
2228 optional<_Tp>>
2229 make_optional(initializer_list<_Up> __il, _Args&&... __args)
2230 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
2231 { return optional<_Tp>( in_place, __il, std::forward<_Args>(__args)... ); }
2232
2233 // Hash.
2234
2235 template<typename _Tp, typename _Up = remove_const_t<_Tp>>
2236 struct __optional_hash
2237#if ! _GLIBCXX_INLINE_VERSION
2238 : public __hash_empty_base<_Up>
2239#endif
2240 {
2241#if __cplusplus < 202002L
2242 using result_type [[__deprecated__]] = size_t;
2243 using argument_type [[__deprecated__]] = optional<_Tp>;
2244#endif
2245
2246 size_t
2247 operator()(const optional<_Tp>& __t) const
2248 noexcept(noexcept(hash<_Up>{}(*__t)))
2249 {
2250 // We pick an arbitrary hash for disengaged optionals which hopefully
2251 // usual values of _Tp won't typically hash to.
2252 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
2253 return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
2254 }
2255 };
2256
2257 template<typename _Tp>
2258 struct hash<optional<_Tp>>
2259 // hash for optional<T&> is disabled because is_hash_enabled_for<T&> is false
2260 : public __conditional_t<__is_hash_enabled_for<remove_const_t<_Tp>>,
2261 __optional_hash<_Tp>,
2262 __hash_not_enabled<_Tp>>
2263 { };
2264
2265 template<typename _Tp>
2266 struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
2267 { };
2268
2269 /// @}
2270
2271#if __cpp_deduction_guides >= 201606
2272 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
2273#endif
2274
2275#ifdef __cpp_lib_optional_range_support // >= C++26
2276 template<typename _Tp>
2277 inline constexpr bool
2279
2280#if __cpp_lib_optional >= 202506L // C++26
2281 template<typename _Tp>
2282 constexpr bool
2283 ranges::enable_borrowed_range<optional<_Tp&>> = true;
2284#endif
2285
2286 template<typename _Tp>
2287 inline constexpr range_format
2288 format_kind<optional<_Tp>> = range_format::disabled;
2289#endif // __cpp_lib_optional_range_support
2290
2291#undef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
2292
2293_GLIBCXX_END_NAMESPACE_VERSION
2294} // namespace std
2295
2296#endif // __cpp_lib_optional
2297
2298#endif // _GLIBCXX_OPTIONAL
constexpr bool enable_view
[range.view] The ranges::enable_view boolean.
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition complex:434
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1886
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2940
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2936
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2714
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:176
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
Definition valarray:1251
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition valarray:1229
constexpr nullopt_t nullopt
Tag to disengage optional objects.
ISO C++ entities toplevel namespace is std.
typename __detail::__cmp3way_res_impl< _Tp, _Up >::type compare_three_way_result_t
[cmp.result], result of three-way comparison
Definition compare:547
constexpr void _Construct(_Tp *__p, _Args &&... __args)
initializer_list
Primary class template hash.
is_constructible
Definition type_traits:1238
is_assignable
Definition type_traits:1357
Base class for all library exceptions.
Definition exception.h:62
[concept.invocable], concept invocable
Definition concepts:383