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 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1148 // 2746. Inconsistency between requirements for emplace between optional and variant
1149 template<typename... _Args>
1150 _GLIBCXX20_CONSTEXPR
1151 enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
1152 emplace(_Args&&... __args)
1153 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1154 {
1155 this->_M_reset();
1156 this->_M_construct(std::forward<_Args>(__args)...);
1157 return this->_M_get();
1158 }
1159
1160 template<typename _Up, typename... _Args>
1161 _GLIBCXX20_CONSTEXPR
1163 _Tp&>
1164 emplace(initializer_list<_Up> __il, _Args&&... __args)
1165 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
1166 _Args...>)
1167 {
1168 this->_M_reset();
1169 this->_M_construct(__il, std::forward<_Args>(__args)...);
1170 return this->_M_get();
1171 }
1172
1173 // Destructor is implicit, implemented in _Optional_base.
1174
1175 // Swap.
1176 _GLIBCXX20_CONSTEXPR void
1177 swap(optional& __other)
1178 noexcept(is_nothrow_move_constructible_v<_Tp>
1179 && is_nothrow_swappable_v<_Tp>)
1180 {
1181 using std::swap;
1182
1183 if (this->_M_is_engaged() && __other._M_is_engaged())
1184 swap(this->_M_get(), __other._M_get());
1185 else if (this->_M_is_engaged())
1186 {
1187 __other._M_construct(std::move(this->_M_get()));
1188 this->_M_destruct();
1189 }
1190 else if (__other._M_is_engaged())
1191 {
1192 this->_M_construct(std::move(__other._M_get()));
1193 __other._M_destruct();
1194 }
1195 }
1196
1197#ifdef __cpp_lib_optional_range_support // >= C++26
1198 // Iterator support.
1199 constexpr iterator begin() noexcept
1200 {
1201 return iterator(
1202 this->_M_is_engaged() ? std::addressof(this->_M_get()) : nullptr
1203 );
1204 }
1205
1206 constexpr const_iterator begin() const noexcept
1207 {
1208 return const_iterator(
1209 this->_M_is_engaged() ? std::addressof(this->_M_get()) : nullptr
1210 );
1211 }
1212
1213 constexpr iterator end() noexcept
1214 {
1215 return begin() + has_value();
1216 }
1217
1218 constexpr const_iterator end() const noexcept
1219 {
1220 return begin() + has_value();
1221 }
1222#endif // __cpp_lib_optional_range_support
1223
1224 // Observers.
1225 constexpr const _Tp*
1226 operator->() const noexcept
1227 {
1228 __glibcxx_assert(this->_M_is_engaged());
1229 return std::__addressof(this->_M_get());
1230 }
1231
1232 constexpr _Tp*
1233 operator->() noexcept
1234 {
1235 __glibcxx_assert(this->_M_is_engaged());
1236 return std::__addressof(this->_M_get());
1237 }
1238
1239 constexpr const _Tp&
1240 operator*() const& noexcept
1241 {
1242 __glibcxx_assert(this->_M_is_engaged());
1243 return this->_M_get();
1244 }
1245
1246 constexpr _Tp&
1247 operator*()& noexcept
1248 {
1249 __glibcxx_assert(this->_M_is_engaged());
1250 return this->_M_get();
1251 }
1252
1253 constexpr _Tp&&
1254 operator*()&& noexcept
1255 {
1256 __glibcxx_assert(this->_M_is_engaged());
1257 return std::move(this->_M_get());
1258 }
1259
1260 constexpr const _Tp&&
1261 operator*() const&& noexcept
1262 {
1263 __glibcxx_assert(this->_M_is_engaged());
1264 return std::move(this->_M_get());
1265 }
1266
1267 constexpr explicit operator bool() const noexcept
1268 { return this->_M_is_engaged(); }
1269
1270 constexpr bool has_value() const noexcept
1271 { return this->_M_is_engaged(); }
1272
1273 constexpr const _Tp&
1274 value() const&
1275 {
1276 if (this->_M_is_engaged())
1277 return this->_M_get();
1278 __throw_bad_optional_access();
1279 }
1280
1281 constexpr _Tp&
1282 value()&
1283 {
1284 if (this->_M_is_engaged())
1285 return this->_M_get();
1286 __throw_bad_optional_access();
1287 }
1288
1289 constexpr _Tp&&
1290 value()&&
1291 {
1292 if (this->_M_is_engaged())
1293 return std::move(this->_M_get());
1294 __throw_bad_optional_access();
1295 }
1296
1297 constexpr const _Tp&&
1298 value() const&&
1299 {
1300 if (this->_M_is_engaged())
1301 return std::move(this->_M_get());
1302 __throw_bad_optional_access();
1303 }
1304
1305 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1306 // 4406. value_or return statement is inconsistent with Mandates
1307 template<typename _Up = remove_cv_t<_Tp>>
1308 constexpr remove_cv_t<_Tp>
1309 value_or(_Up&& __u) const&
1310 {
1311 using _Xp = remove_cv_t<_Tp>;
1312 static_assert(is_convertible_v<const _Tp&, _Xp>);
1313 static_assert(is_convertible_v<_Up, _Xp>);
1314
1315 if (this->_M_is_engaged())
1316 return this->_M_get();
1317 return std::forward<_Up>(__u);
1318 }
1319
1320 template<typename _Up = remove_cv_t<_Tp>>
1321 constexpr remove_cv_t<_Tp>
1322 value_or(_Up&& __u) &&
1323 {
1324 using _Xp = remove_cv_t<_Tp>;
1325 static_assert(is_convertible_v<_Tp, _Xp>);
1326 static_assert(is_convertible_v<_Up, _Xp>);
1327
1328 if (this->_M_is_engaged())
1329 return std::move(this->_M_get());
1330 return std::forward<_Up>(__u);
1331 }
1332
1333#if __cpp_lib_optional >= 202110L // C++23
1334 // [optional.monadic]
1335
1336 template<typename _Fn>
1337 constexpr auto
1338 and_then(_Fn&& __f) &
1339 {
1340 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp&>>;
1341 static_assert(__is_optional_v<_Up>,
1342 "the function passed to std::optional<T>::and_then "
1343 "must return a std::optional");
1344 if (has_value())
1345 return std::__invoke(std::forward<_Fn>(__f), _M_get());
1346 else
1347 return _Up();
1348 }
1349
1350 template<typename _Fn>
1351 constexpr auto
1352 and_then(_Fn&& __f) const &
1353 {
1354 using _Up = remove_cvref_t<invoke_result_t<_Fn, const _Tp&>>;
1355 static_assert(__is_optional_v<_Up>,
1356 "the function passed to std::optional<T>::and_then "
1357 "must return a std::optional");
1358 if (has_value())
1359 return std::__invoke(std::forward<_Fn>(__f), _M_get());
1360 else
1361 return _Up();
1362 }
1363
1364 template<typename _Fn>
1365 constexpr auto
1366 and_then(_Fn&& __f) &&
1367 {
1368 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp>>;
1369 static_assert(__is_optional_v<_Up>,
1370 "the function passed to std::optional<T>::and_then "
1371 "must return a std::optional");
1372 if (has_value())
1373 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_get()));
1374 else
1375 return _Up();
1376 }
1377
1378 template<typename _Fn>
1379 constexpr auto
1380 and_then(_Fn&& __f) const &&
1381 {
1382 using _Up = remove_cvref_t<invoke_result_t<_Fn, const _Tp>>;
1383 static_assert(__is_optional_v<_Up>,
1384 "the function passed to std::optional<T>::and_then "
1385 "must return a std::optional");
1386 if (has_value())
1387 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_get()));
1388 else
1389 return _Up();
1390 }
1391
1392 template<typename _Fn>
1393 constexpr auto
1394 transform(_Fn&& __f) &
1395 {
1396 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp&>>;
1397 if (has_value())
1398 return optional<_Up>(_Optional_func<_Fn>{__f}, _M_get());
1399 else
1400 return optional<_Up>();
1401 }
1402
1403 template<typename _Fn>
1404 constexpr auto
1405 transform(_Fn&& __f) const &
1406 {
1407 using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp&>>;
1408 if (has_value())
1409 return optional<_Up>(_Optional_func<_Fn>{__f}, _M_get());
1410 else
1411 return optional<_Up>();
1412 }
1413
1414 template<typename _Fn>
1415 constexpr auto
1416 transform(_Fn&& __f) &&
1417 {
1418 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp>>;
1419 if (has_value())
1420 return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(_M_get()));
1421 else
1422 return optional<_Up>();
1423 }
1424
1425 template<typename _Fn>
1426 constexpr auto
1427 transform(_Fn&& __f) const &&
1428 {
1429 using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp>>;
1430 if (has_value())
1431 return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(_M_get()));
1432 else
1433 return optional<_Up>();
1434 }
1435
1436 template<typename _Fn> requires invocable<_Fn> && copy_constructible<_Tp>
1437 constexpr optional
1438 or_else(_Fn&& __f) const&
1439 {
1440 using _Up = invoke_result_t<_Fn>;
1441 static_assert(is_same_v<remove_cvref_t<_Up>, optional>,
1442 "the function passed to std::optional<T>::or_else "
1443 "must return a std::optional<T>");
1444
1445 if (has_value())
1446 return *this;
1447 else
1448 return std::forward<_Fn>(__f)();
1449 }
1450
1451 template<typename _Fn> requires invocable<_Fn> && move_constructible<_Tp>
1452 constexpr optional
1453 or_else(_Fn&& __f) &&
1454 {
1455 using _Up = invoke_result_t<_Fn>;
1456 static_assert(is_same_v<remove_cvref_t<_Up>, optional>,
1457 "the function passed to std::optional<T>::or_else "
1458 "must return a std::optional<T>");
1459
1460 if (has_value())
1461 return std::move(*this);
1462 else
1463 return std::forward<_Fn>(__f)();
1464 }
1465#endif
1466
1467 _GLIBCXX20_CONSTEXPR void reset() noexcept { this->_M_reset(); }
1468
1469 private:
1470 using _Base::_M_get;
1471
1472 [[__gnu__::__always_inline__]]
1473 constexpr _Tp&
1474 _M_fwd() & noexcept
1475 { return _M_get(); }
1476
1477 [[__gnu__::__always_inline__]]
1478 constexpr _Tp&&
1479 _M_fwd() && noexcept
1480 { return std::move(_M_get()); }
1481
1482 [[__gnu__::__always_inline__]]
1483 constexpr const _Tp&
1484 _M_fwd() const& noexcept
1485 { return _M_get(); }
1486
1487 [[__gnu__::__always_inline__]]
1488 constexpr const _Tp&&
1489 _M_fwd() const&& noexcept
1490 { return std::move(_M_get()); }
1491
1492 template<typename _Up> friend class optional;
1493
1494#if __cpp_lib_optional >= 202110L // C++23
1495 template<typename _Fn, typename _Value>
1496 explicit constexpr
1497 optional(_Optional_func<_Fn> __f, _Value&& __v)
1498 {
1499 this->_M_payload._M_apply(__f, std::forward<_Value>(__v));
1500 }
1501#endif
1502 };
1503
1504#if __cpp_lib_optional >= 202506L // C++26
1505 template<typename _Tp>
1506 class optional<_Tp&>;
1507
1508 template<typename _Tp>
1509 constexpr bool __is_optional_ref_v = false;
1510
1511 template<typename _Tp>
1512 constexpr bool __is_optional_ref_v<optional<_Tp&>> = true;
1513
1514 template<typename _Tp>
1515 struct __optional_ref_base
1516 {};
1517
1518#ifdef __cpp_lib_optional_range_support // >= C++26
1519 template<typename _Tp>
1520 struct __optional_ref_base<_Tp[]>
1521 {};
1522
1523 template<typename _Tp>
1524 requires is_object_v<_Tp>
1525 struct __optional_ref_base<_Tp>
1526 {
1527 using iterator = __gnu_cxx::__normal_iterator<_Tp*, optional<_Tp&>>;
1528 };
1529#endif // __cpp_lib_optional_range_support
1530
1531 template<typename _Tp>
1532 class optional<_Tp&> : public __optional_ref_base<_Tp>
1533 {
1534 static_assert(__is_valid_contained_type_for_optional<_Tp&>);
1535
1536 public:
1537 using value_type = _Tp;
1538
1539 // Constructors.
1540 constexpr optional() noexcept = default;
1541 constexpr optional(nullopt_t) noexcept : optional() {}
1542 constexpr optional(const optional&) noexcept = default;
1543
1544 template<typename _Arg>
1545 requires is_constructible_v<_Tp&, _Arg>
1546 && (!reference_constructs_from_temporary_v<_Tp&, _Arg>)
1547 explicit constexpr
1548 optional(in_place_t, _Arg&& __arg)
1549 {
1550 __convert_ref_init_val(std::forward<_Arg>(__arg));
1551 }
1552
1553 template<typename _Up>
1554 requires (!is_same_v<remove_cvref_t<_Up>, optional>)
1555 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
1556 && is_constructible_v<_Tp&, _Up>
1557 && (!reference_constructs_from_temporary_v<_Tp&, _Up>)
1558 explicit(!is_convertible_v<_Up, _Tp&>)
1559 constexpr
1560 optional(_Up&& __u)
1561 noexcept(is_nothrow_constructible_v<_Tp&, _Up>)
1562 {
1563 __convert_ref_init_val(std::forward<_Up>(__u));
1564 }
1565
1566 template<typename _Up>
1567 requires (!is_same_v<remove_cvref_t<_Up>, optional>)
1568 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
1569 && is_constructible_v<_Tp&, _Up>
1570 && reference_constructs_from_temporary_v<_Tp&, _Up>
1571 explicit(!is_convertible_v<_Up, _Tp&>)
1572 constexpr
1573 optional(_Up&& __u) = delete;
1574
1575 // optional<U> &
1576 template<typename _Up>
1577 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1578 && (!is_same_v<_Tp&, _Up>)
1579 && is_constructible_v<_Tp&, _Up&>
1580 && (!reference_constructs_from_temporary_v<_Tp&, _Up&>)
1581 explicit(!is_convertible_v<_Up&, _Tp&>)
1582 constexpr
1583 optional(optional<_Up>& __rhs)
1584 noexcept(is_nothrow_constructible_v<_Tp&, _Up&>)
1585 {
1586 if (__rhs)
1587 __convert_ref_init_val(__rhs._M_fwd());
1588 }
1589
1590 template<typename _Up>
1591 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1592 && (!is_same_v<_Tp&, _Up>)
1593 && is_constructible_v<_Tp&, _Up&>
1594 && reference_constructs_from_temporary_v<_Tp&, _Up&>
1595 explicit(!is_convertible_v<_Up&, _Tp&>)
1596 constexpr
1597 optional(optional<_Up>& __rhs) = delete;
1598
1599 // const optional<U>&
1600 template<typename _Up>
1601 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1602 && (!is_same_v<_Tp&, _Up>)
1603 && is_constructible_v<_Tp&, const _Up&>
1604 && (!reference_constructs_from_temporary_v<_Tp&, const _Up&>)
1605 explicit(!is_convertible_v<const _Up&, _Tp&>)
1606 constexpr
1607 optional(const optional<_Up>& __rhs)
1608 noexcept(is_nothrow_constructible_v<_Tp&, _Up&>)
1609 {
1610 if (__rhs)
1611 __convert_ref_init_val(__rhs._M_fwd());
1612 }
1613
1614 template<typename _Up>
1615 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1616 && (!is_same_v<_Tp&, _Up>)
1617 && is_constructible_v<_Tp&, const _Up&>
1618 && reference_constructs_from_temporary_v<_Tp&, const _Up&>
1619 explicit(!is_convertible_v<const _Up&, _Tp&>)
1620 constexpr
1621 optional(const optional<_Up>& __rhs) = delete;
1622
1623 // optional<U>&&
1624 template<typename _Up>
1625 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1626 && (!is_same_v<_Tp&, _Up>)
1627 && is_constructible_v<_Tp&, _Up>
1628 && (!reference_constructs_from_temporary_v<_Tp&, _Up>)
1629 explicit(!is_convertible_v<_Up, _Tp&>)
1630 constexpr
1631 optional(optional<_Up>&& __rhs)
1632 noexcept(is_nothrow_constructible_v<_Tp&, _Up>)
1633 {
1634 if (__rhs)
1635 __convert_ref_init_val(std::move(__rhs)._M_fwd());
1636 }
1637
1638 template<typename _Up>
1639 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1640 && (!is_same_v<_Tp&, _Up>)
1641 && is_constructible_v<_Tp&, _Up>
1642 && reference_constructs_from_temporary_v<_Tp&, _Up>
1643 explicit(!is_convertible_v<_Up, _Tp&>)
1644 constexpr
1645 optional(optional<_Up>&& __rhs) = delete;
1646
1647 // const optional<U>&&
1648 template<typename _Up>
1649 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1650 && (!is_same_v<_Tp&, _Up>)
1651 && is_constructible_v<_Tp&, const _Up>
1652 && (!reference_constructs_from_temporary_v<_Tp&, _Up>)
1653 explicit(!is_convertible_v<const _Up, _Tp&>)
1654 constexpr
1655 optional(const optional<_Up>&& __rhs)
1656 noexcept(is_nothrow_constructible_v<_Tp&, const _Up>)
1657 {
1658 if (__rhs)
1659 __convert_ref_init_val(std::move(__rhs)._M_fwd());
1660 }
1661
1662 template<typename _Up>
1663 requires (!is_same_v<remove_cv_t<_Tp>, optional<_Up>>)
1664 && (!is_same_v<_Tp&, _Up>)
1665 && is_constructible_v<_Tp&, const _Up>
1666 && reference_constructs_from_temporary_v<_Tp&, const _Up>
1667 explicit(!is_convertible_v<const _Up, _Tp&>)
1668 constexpr
1669 optional(const optional<_Up>&& __rhs) = delete;
1670
1671 constexpr ~optional() = default;
1672
1673 // Assignment.
1674 constexpr optional& operator=(nullopt_t) noexcept
1675 {
1676 _M_val = nullptr;
1677 return *this;
1678 }
1679
1680 constexpr optional& operator=(const optional&) noexcept = default;
1681
1682 template<typename _Up>
1683 requires is_constructible_v<_Tp&, _Up>
1684 && (!reference_constructs_from_temporary_v<_Tp&, _Up>)
1685 constexpr _Tp&
1686 emplace(_Up&& __u)
1687 noexcept(is_nothrow_constructible_v<_Tp&, _Up>)
1688 {
1689 __convert_ref_init_val(std::forward<_Up>(__u));
1690 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1691 // 4300. Missing Returns: element in optional<T&>::emplace
1692 return *_M_val;
1693 }
1694
1695 // Swap.
1696 constexpr void swap(optional& __rhs) noexcept
1697 { std::swap(_M_val, __rhs._M_val); }
1698
1699#ifdef __cpp_lib_optional_range_support // >= C++26
1700 // Iterator support.
1701 constexpr auto begin() const noexcept
1702 requires is_object_v<_Tp> && (!is_unbounded_array_v<_Tp>)
1703 { return __gnu_cxx::__normal_iterator<_Tp*, optional>(_M_val); }
1704
1705 constexpr auto end() const noexcept
1706 requires is_object_v<_Tp> && (!is_unbounded_array_v<_Tp>)
1707 { return begin() + has_value(); }
1708#endif // __cpp_lib_optional_range_support
1709
1710 // Observers.
1711 constexpr _Tp* operator->() const noexcept
1712 {
1713 __glibcxx_assert(_M_val); // hardened precondition
1714 return _M_val;
1715 }
1716
1717 constexpr _Tp& operator*() const noexcept
1718 {
1719 __glibcxx_assert(_M_val); // hardened precondition
1720 return *_M_val;
1721 }
1722
1723 constexpr explicit operator bool() const noexcept
1724 {
1725 return _M_val;
1726 }
1727
1728 constexpr bool has_value() const noexcept
1729 {
1730 return _M_val;
1731 }
1732
1733 constexpr _Tp& value() const
1734 {
1735 if (_M_val)
1736 return *_M_val;
1737 __throw_bad_optional_access();
1738 }
1739
1740 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1741 // 4304. std::optional<NonReturnable&> is ill-formed due to value_or
1742 template<typename _Up = remove_cv_t<_Tp>>
1743 requires is_object_v<_Tp> && (!is_array_v<_Tp>)
1744 constexpr decay_t<_Tp>
1745 value_or(_Up&& __u) const
1746 {
1747 using _Xp = remove_cv_t<_Tp>;
1748 static_assert(is_convertible_v<_Tp&, _Xp>);
1749 static_assert(is_convertible_v<_Up, _Xp>);
1750 if (_M_val)
1751 return *_M_val;
1752 return std::forward<_Up>(__u);
1753 }
1754
1755 // Monadic operations.
1756 template<typename _Fn>
1757 constexpr auto
1758 and_then(_Fn&& __f) const
1759 {
1760 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp&>>;
1761 static_assert(__is_optional_v<_Up>,
1762 "the function passed to std::optional<T&>::and_then "
1763 "must return a std::optional");
1764 if (has_value())
1765 return std::__invoke(std::forward<_Fn>(__f), *_M_val);
1766 else
1767 return _Up();
1768 }
1769
1770 template<typename _Fn>
1771 constexpr
1772 optional<remove_cv_t<invoke_result_t<_Fn, _Tp&>>>
1773 transform(_Fn&& __f) const
1774 {
1775 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp&>>;
1776 if (has_value())
1777 return optional<_Up>(_Optional_func<_Fn>{__f}, *_M_val);
1778 else
1779 return optional<_Up>();
1780 }
1781
1782 template<typename _Fn>
1783 requires invocable<_Fn>
1784 constexpr
1785 optional
1786 or_else(_Fn&& __f) const
1787 {
1788 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Fn>>, optional>,
1789 "the function passed to std::optional<T&>::or_else "
1790 "must return a std::optional<T&>");
1791 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1792 // 4367. Improve optional<T&>::or_else
1793 if (has_value())
1794 return *this;
1795 else
1796 return std::forward<_Fn>(__f)();
1797 }
1798
1799 // Modifiers.
1800 constexpr void reset() noexcept
1801 {
1802 _M_val = nullptr;
1803 }
1804
1805 private:
1806 _Tp *_M_val = nullptr;
1807
1808 [[__gnu__::__always_inline__]]
1809 constexpr _Tp&
1810 _M_fwd() const noexcept
1811 { return *_M_val; }
1812
1813 template<typename _Up> friend class optional;
1814
1815 template<typename _Up>
1816 constexpr
1817 void
1818 __convert_ref_init_val(_Up&& __u)
1819 noexcept
1820 {
1821 _Tp& __r(std::forward<_Up>(__u));
1822 _M_val = std::addressof(__r);
1823 }
1824
1825 template<typename _Fn, typename _Value>
1826 explicit constexpr
1827 optional(_Optional_func<_Fn> __f, _Value&& __v)
1828 {
1829 _Tp& __r = std::__invoke(std::forward<_Fn>(__f._M_f), std::forward<_Value>(__v));
1830 _M_val = std::addressof(__r);
1831 }
1832 };
1833#endif // __cpp_lib_optional >= 202506L
1834
1835 template<typename _Tp>
1836 using __optional_relop_t =
1838
1839 template<typename _Tp, typename _Up>
1840 using __optional_eq_t = __optional_relop_t<
1842 >;
1843
1844 template<typename _Tp, typename _Up>
1845 using __optional_ne_t = __optional_relop_t<
1847 >;
1848
1849 template<typename _Tp, typename _Up>
1850 using __optional_lt_t = __optional_relop_t<
1852 >;
1853
1854 template<typename _Tp, typename _Up>
1855 using __optional_gt_t = __optional_relop_t<
1857 >;
1858
1859 template<typename _Tp, typename _Up>
1860 using __optional_le_t = __optional_relop_t<
1862 >;
1863
1864 template<typename _Tp, typename _Up>
1865 using __optional_ge_t = __optional_relop_t<
1867 >;
1868
1869 // Comparisons between optional values.
1870 template<typename _Tp, typename _Up>
1871 constexpr auto
1872 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1873 -> __optional_eq_t<_Tp, _Up>
1874 {
1875 if (__lhs.has_value() != __rhs.has_value())
1876 return false;
1877 if (!__lhs.has_value())
1878 return true;
1879 return *__lhs == *__rhs;
1880 }
1881
1882 template<typename _Tp, typename _Up>
1883 constexpr auto
1884 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1885 -> __optional_ne_t<_Tp, _Up>
1886 {
1887 if (__lhs.has_value() != __rhs.has_value())
1888 return true;
1889 if (!__lhs.has_value())
1890 return false;
1891 return *__lhs != *__rhs;
1892 }
1893
1894 template<typename _Tp, typename _Up>
1895 constexpr auto
1896 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1897 -> __optional_lt_t<_Tp, _Up>
1898 {
1899 if (!__rhs.has_value())
1900 return false;
1901 if (!__lhs.has_value())
1902 return true;
1903 return *__lhs < *__rhs;
1904 }
1905
1906 template<typename _Tp, typename _Up>
1907 constexpr auto
1908 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1909 -> __optional_gt_t<_Tp, _Up>
1910 {
1911 if (!__lhs.has_value())
1912 return false;
1913 if (!__rhs.has_value())
1914 return true;
1915 return *__lhs > *__rhs;
1916 }
1917
1918 template<typename _Tp, typename _Up>
1919 constexpr auto
1920 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1921 -> __optional_le_t<_Tp, _Up>
1922 {
1923 if (!__lhs.has_value())
1924 return true;
1925 if (!__rhs.has_value())
1926 return false;
1927 return *__lhs <= *__rhs;
1928 }
1929
1930 template<typename _Tp, typename _Up>
1931 constexpr auto
1932 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1933 -> __optional_ge_t<_Tp, _Up>
1934 {
1935 if (!__rhs.has_value())
1936 return true;
1937 if (!__lhs.has_value())
1938 return false;
1939 return *__lhs >= *__rhs;
1940 }
1941
1942#ifdef __cpp_lib_three_way_comparison
1943 template<typename _Tp, three_way_comparable_with<_Tp> _Up>
1944 [[nodiscard]]
1946 operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
1947 {
1948 return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
1949 }
1950#endif
1951
1952 // Comparisons with nullopt.
1953 template<typename _Tp>
1954 [[nodiscard]]
1955 constexpr bool
1956 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
1957 { return !__lhs; }
1958
1959#ifdef __cpp_lib_three_way_comparison
1960 template<typename _Tp>
1961 [[nodiscard]]
1962 constexpr strong_ordering
1963 operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
1964 { return bool(__x) <=> false; }
1965#else
1966 template<typename _Tp>
1967 constexpr bool
1968 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
1969 { return !__rhs; }
1970
1971 template<typename _Tp>
1972 constexpr bool
1973 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1974 { return static_cast<bool>(__lhs); }
1975
1976 template<typename _Tp>
1977 constexpr bool
1978 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1979 { return static_cast<bool>(__rhs); }
1980
1981 template<typename _Tp>
1982 constexpr bool
1983 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1984 { return false; }
1985
1986 template<typename _Tp>
1987 constexpr bool
1988 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
1989 { return static_cast<bool>(__rhs); }
1990
1991 template<typename _Tp>
1992 constexpr bool
1993 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
1994 { return static_cast<bool>(__lhs); }
1995
1996 template<typename _Tp>
1997 constexpr bool
1998 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1999 { return false; }
2000
2001 template<typename _Tp>
2002 constexpr bool
2003 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
2004 { return !__lhs; }
2005
2006 template<typename _Tp>
2007 constexpr bool
2008 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
2009 { return true; }
2010
2011 template<typename _Tp>
2012 constexpr bool
2013 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
2014 { return true; }
2015
2016 template<typename _Tp>
2017 constexpr bool
2018 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
2019 { return !__rhs; }
2020#endif // three-way-comparison
2021
2022#if __cpp_lib_concepts
2023 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2024 // 4072. std::optional comparisons: constrain harder
2025# define _REQUIRES_NOT_OPTIONAL(T) requires (!__is_optional_v<T>)
2026#else
2027# define _REQUIRES_NOT_OPTIONAL(T)
2028#endif
2029
2030 // Comparisons with value type.
2031 template<typename _Tp, typename _Up>
2032 _REQUIRES_NOT_OPTIONAL(_Up)
2033 constexpr auto
2034 operator== [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2035 -> __optional_eq_t<_Tp, _Up>
2036 {
2037 if (__lhs.has_value())
2038 return *__lhs == __rhs;
2039 return false;
2040 }
2041
2042 template<typename _Tp, typename _Up>
2043 _REQUIRES_NOT_OPTIONAL(_Tp)
2044 constexpr auto
2045 operator== [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2046 -> __optional_eq_t<_Tp, _Up>
2047 {
2048 if (__rhs.has_value())
2049 return __lhs == *__rhs;
2050 return false;
2051 }
2052
2053 template<typename _Tp, typename _Up>
2054 _REQUIRES_NOT_OPTIONAL(_Up)
2055 constexpr auto
2056 operator!= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2057 -> __optional_ne_t<_Tp, _Up>
2058 {
2059 if (__lhs.has_value())
2060 return *__lhs != __rhs;
2061 return true;
2062 }
2063
2064 template<typename _Tp, typename _Up>
2065 _REQUIRES_NOT_OPTIONAL(_Tp)
2066 constexpr auto
2067 operator!= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2068 -> __optional_ne_t<_Tp, _Up>
2069 {
2070 if (__rhs.has_value())
2071 return __lhs != *__rhs;
2072 return true;
2073 }
2074
2075 template<typename _Tp, typename _Up>
2076 _REQUIRES_NOT_OPTIONAL(_Up)
2077 constexpr auto
2078 operator< [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2079 -> __optional_lt_t<_Tp, _Up>
2080 {
2081 if (__lhs.has_value())
2082 return *__lhs < __rhs;
2083 return true;
2084 }
2085
2086 template<typename _Tp, typename _Up>
2087 _REQUIRES_NOT_OPTIONAL(_Tp)
2088 constexpr auto
2089 operator< [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2090 -> __optional_lt_t<_Tp, _Up>
2091 {
2092 if (__rhs.has_value())
2093 return __lhs < *__rhs;
2094 return false;
2095 }
2096
2097 template<typename _Tp, typename _Up>
2098 _REQUIRES_NOT_OPTIONAL(_Up)
2099 constexpr auto
2100 operator> [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2101 -> __optional_gt_t<_Tp, _Up>
2102 {
2103 if (__lhs.has_value())
2104 return *__lhs > __rhs;
2105 return false;
2106 }
2107
2108 template<typename _Tp, typename _Up>
2109 _REQUIRES_NOT_OPTIONAL(_Tp)
2110 constexpr auto
2111 operator> [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2112 -> __optional_gt_t<_Tp, _Up>
2113 {
2114 if (__rhs.has_value())
2115 return __lhs > *__rhs;
2116 return true;
2117 }
2118
2119 template<typename _Tp, typename _Up>
2120 _REQUIRES_NOT_OPTIONAL(_Up)
2121 constexpr auto
2122 operator<= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2123 -> __optional_le_t<_Tp, _Up>
2124 {
2125 if (__lhs.has_value())
2126 return *__lhs <= __rhs;
2127 return true;
2128 }
2129
2130 template<typename _Tp, typename _Up>
2131 _REQUIRES_NOT_OPTIONAL(_Tp)
2132 constexpr auto
2133 operator<= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2134 -> __optional_le_t<_Tp, _Up>
2135 {
2136 if (__rhs.has_value())
2137 return __lhs <= *__rhs;
2138 return false;
2139 }
2140
2141 template<typename _Tp, typename _Up>
2142 _REQUIRES_NOT_OPTIONAL(_Up)
2143 constexpr auto
2144 operator>= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
2145 -> __optional_ge_t<_Tp, _Up>
2146 {
2147 if (__lhs.has_value())
2148 return *__lhs >= __rhs;
2149 return false;
2150 }
2151
2152 template<typename _Tp, typename _Up>
2153 _REQUIRES_NOT_OPTIONAL(_Tp)
2154 constexpr auto
2155 operator>= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
2156 -> __optional_ge_t<_Tp, _Up>
2157 {
2158 if (__rhs.has_value())
2159 return __lhs >= *__rhs;
2160 return true;
2161 }
2162
2163#ifdef __cpp_lib_three_way_comparison
2164 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2165 // 3746. optional's spaceship with U with a type derived from optional
2166 // causes infinite constraint meta-recursion
2167 template<typename _Tp>
2168 concept __is_derived_from_optional = requires (const _Tp& __t) {
2169 []<typename _Up>(const optional<_Up>&){ }(__t);
2170 };
2171
2172 template<typename _Tp, typename _Up>
2173 requires (!__is_derived_from_optional<_Up>)
2174 && requires { typename compare_three_way_result_t<_Tp, _Up>; }
2175 && three_way_comparable_with<_Tp, _Up>
2177 operator<=> [[nodiscard]] (const optional<_Tp>& __x, const _Up& __v)
2178 { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
2179#endif
2180
2181 // Swap and creation functions.
2182
2183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2184 // 2748. swappable traits for optionals
2185 template<typename _Tp>
2186 _GLIBCXX20_CONSTEXPR
2187 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
2188 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
2189 noexcept(noexcept(__lhs.swap(__rhs)))
2190 { __lhs.swap(__rhs); }
2191
2192#if __cpp_lib_optional >= 202506L
2193 // We deviate from standard, that do not declared separate swap overload
2194 // from optional<T&>.
2195 template<typename _Tp>
2196 constexpr void
2197 swap(optional<_Tp&>& __lhs, optional<_Tp&>& __rhs) noexcept
2198 { __lhs.swap(__rhs); }
2199#endif
2200
2201 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2202 // 2766. Swapping non-swappable types
2203 template<typename _Tp>
2204 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
2205 swap(optional<_Tp>&, optional<_Tp>&) = delete;
2206
2207#if __cpp_lib_optional >= 202506L
2208 template<int = 0, typename _Tp>
2209#else
2210 template<typename _Tp>
2211#endif
2212 constexpr
2214 optional<decay_t<_Tp>>>
2215 make_optional(_Tp&& __t)
2216 noexcept(is_nothrow_constructible_v<optional<decay_t<_Tp>>, _Tp>)
2217 { return optional<decay_t<_Tp>>( std::forward<_Tp>(__t) ); }
2218
2219 template<typename _Tp, typename... _Args>
2220 constexpr
2221 enable_if_t<is_constructible_v<_Tp, _Args...>,
2222 optional<_Tp>>
2223 make_optional(_Args&&... __args)
2224 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
2225 { return optional<_Tp>( in_place, std::forward<_Args>(__args)... ); }
2226
2227 template<typename _Tp, typename _Up, typename... _Args>
2228 constexpr
2230 optional<_Tp>>
2231 make_optional(initializer_list<_Up> __il, _Args&&... __args)
2232 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
2233 { return optional<_Tp>( in_place, __il, std::forward<_Args>(__args)... ); }
2234
2235 // Hash.
2236
2237 template<typename _Tp, typename _Up = remove_const_t<_Tp>>
2238 struct __optional_hash
2239#if ! _GLIBCXX_INLINE_VERSION
2240 : public __hash_empty_base<_Up>
2241#endif
2242 {
2243#if __cplusplus < 202002L
2244 using result_type [[__deprecated__]] = size_t;
2245 using argument_type [[__deprecated__]] = optional<_Tp>;
2246#endif
2247
2248 size_t
2249 operator()(const optional<_Tp>& __t) const
2250 noexcept(noexcept(hash<_Up>{}(*__t)))
2251 {
2252 // We pick an arbitrary hash for disengaged optionals which hopefully
2253 // usual values of _Tp won't typically hash to.
2254 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
2255 return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
2256 }
2257 };
2258
2259 template<typename _Tp>
2260 struct hash<optional<_Tp>>
2261 // hash for optional<T&> is disabled because is_hash_enabled_for<T&> is false
2262 : public __conditional_t<__is_hash_enabled_for<remove_const_t<_Tp>>,
2263 __optional_hash<_Tp>,
2264 __hash_not_enabled<_Tp>>
2265 { };
2266
2267 template<typename _Tp>
2268 struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
2269 { };
2270
2271 /// @}
2272
2273#if __cpp_deduction_guides >= 201606
2274 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
2275#endif
2276
2277#ifdef __cpp_lib_optional_range_support // >= C++26
2278 template<typename _Tp>
2279 inline constexpr bool
2281
2282#if __cpp_lib_optional >= 202506L // C++26
2283 template<typename _Tp>
2284 constexpr bool
2285 ranges::enable_borrowed_range<optional<_Tp&>> = true;
2286#endif
2287
2288 template<typename _Tp>
2289 inline constexpr range_format
2290 format_kind<optional<_Tp>> = range_format::disabled;
2291#endif // __cpp_lib_optional_range_support
2292
2293#undef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
2294
2295_GLIBCXX_END_NAMESPACE_VERSION
2296} // namespace std
2297
2298#endif // __cpp_lib_optional
2299
2300#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