libstdc++
optional
Go to the documentation of this file.
1// <optional> -*- C++ -*-
2
3// Copyright (C) 2013-2024 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#pragma GCC system_header
34
35#define __glibcxx_want_freestanding_optional
36#define __glibcxx_want_optional
37#define __glibcxx_want_constrained_equality
38#include <bits/version.h>
39
40#ifdef __cpp_lib_optional // C++ >= 17
41
42#include <type_traits>
43#include <exception>
44#include <new>
45#include <initializer_list>
49#include <bits/stl_construct.h> // _Construct
50#include <bits/utility.h> // in_place_t
51#if __cplusplus > 201703L
52# include <compare>
53# include <bits/invoke.h> // std::__invoke
54#endif
55#if __cplusplus > 202002L
56# include <concepts>
57#endif
58
59namespace std _GLIBCXX_VISIBILITY(default)
60{
61_GLIBCXX_BEGIN_NAMESPACE_VERSION
62
63 /**
64 * @addtogroup utilities
65 * @{
66 */
67
68 template<typename _Tp>
69 class optional;
70
71 /// Tag type to disengage optional objects.
72 struct nullopt_t
73 {
74 // Do not user-declare default constructor at all for
75 // optional_value = {} syntax to work.
76 // nullopt_t() = delete;
77
78 // Used for constructing nullopt.
79 enum class _Construct { _Token };
80
81 // Must be constexpr for nullopt_t to be literal.
82 explicit constexpr nullopt_t(_Construct) noexcept { }
83 };
84
85 /// Tag to disengage optional objects.
86 inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
87
88 template<typename _Fn> struct _Optional_func { _Fn& _M_f; };
89
90 /**
91 * @brief Exception class thrown when a disengaged optional object is
92 * dereferenced.
93 * @ingroup exceptions
94 */
95 class bad_optional_access : public exception
96 {
97 public:
98 bad_optional_access() = default;
99 virtual ~bad_optional_access() = default;
100
101 const char* what() const noexcept override
102 { return "bad optional access"; }
103 };
104
105 // XXX Does not belong here.
106 [[__noreturn__]] inline void
107 __throw_bad_optional_access()
108 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
109
110 // This class template manages construction/destruction of
111 // the contained value for a std::optional.
112 template <typename _Tp>
113 struct _Optional_payload_base
114 {
115 using _Stored_type = remove_const_t<_Tp>;
116
117 _Optional_payload_base() = default;
118 ~_Optional_payload_base() = default;
119
120 template<typename... _Args>
121 constexpr
122 _Optional_payload_base(in_place_t __tag, _Args&&... __args)
123 : _M_payload(__tag, std::forward<_Args>(__args)...),
124 _M_engaged(true)
125 { }
126
127 template<typename _Up, typename... _Args>
128 constexpr
129 _Optional_payload_base(std::initializer_list<_Up> __il,
130 _Args&&... __args)
131 : _M_payload(__il, std::forward<_Args>(__args)...),
132 _M_engaged(true)
133 { }
134
135 // Constructor used by _Optional_base copy constructor when the
136 // contained value is not trivially copy constructible.
137 constexpr
138 _Optional_payload_base(bool /* __engaged */,
139 const _Optional_payload_base& __other)
140 {
141 if (__other._M_engaged)
142 this->_M_construct(__other._M_get());
143 }
144
145 // Constructor used by _Optional_base move constructor when the
146 // contained value is not trivially move constructible.
147 constexpr
148 _Optional_payload_base(bool /* __engaged */,
149 _Optional_payload_base&& __other)
150 {
151 if (__other._M_engaged)
152 this->_M_construct(std::move(__other._M_get()));
153 }
154
155 // Copy constructor is only used to when the contained value is
156 // trivially copy constructible.
157 _Optional_payload_base(const _Optional_payload_base&) = default;
158
159 // Move constructor is only used to when the contained value is
160 // trivially copy constructible.
161 _Optional_payload_base(_Optional_payload_base&&) = default;
162
163 _Optional_payload_base&
164 operator=(const _Optional_payload_base&) = default;
165
166 _Optional_payload_base&
167 operator=(_Optional_payload_base&&) = default;
168
169 // used to perform non-trivial copy assignment.
170 constexpr void
171 _M_copy_assign(const _Optional_payload_base& __other)
172 {
173 if (this->_M_engaged && __other._M_engaged)
174 this->_M_get() = __other._M_get();
175 else
176 {
177 if (__other._M_engaged)
178 this->_M_construct(__other._M_get());
179 else
180 this->_M_reset();
181 }
182 }
183
184 // used to perform non-trivial move assignment.
185 constexpr void
186 _M_move_assign(_Optional_payload_base&& __other)
187 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
188 is_nothrow_move_assignable<_Tp>>)
189 {
190 if (this->_M_engaged && __other._M_engaged)
191 this->_M_get() = std::move(__other._M_get());
192 else
193 {
194 if (__other._M_engaged)
195 this->_M_construct(std::move(__other._M_get()));
196 else
197 this->_M_reset();
198 }
199 }
200
201 struct _Empty_byte { };
202
203 template<typename _Up, bool = is_trivially_destructible_v<_Up>>
204 union _Storage
205 {
206 constexpr _Storage() noexcept : _M_empty() { }
207
208 template<typename... _Args>
209 constexpr
210 _Storage(in_place_t, _Args&&... __args)
211 : _M_value(std::forward<_Args>(__args)...)
212 { }
213
214 template<typename _Vp, typename... _Args>
215 constexpr
216 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
217 : _M_value(__il, std::forward<_Args>(__args)...)
218 { }
219
220#if __cplusplus >= 202002L
221 template<typename _Fn, typename _Arg>
222 constexpr
223 _Storage(_Optional_func<_Fn> __f, _Arg&& __arg)
224 : _M_value(std::__invoke(std::forward<_Fn>(__f._M_f),
225 std::forward<_Arg>(__arg)))
226 { }
227#endif
228
229#if __cpp_concepts >= 202002L // Conditionally trivial special member functions
230 ~_Storage() = default;
231
232 // User-provided destructor is needed when _Up has non-trivial dtor.
233 _GLIBCXX20_CONSTEXPR
234 ~_Storage() requires (!is_trivially_destructible_v<_Up>)
235 { }
236
237 _Storage(const _Storage&) = default;
238 _Storage(_Storage&&) = default;
239 _Storage& operator=(const _Storage&) = default;
240 _Storage& operator=(_Storage&&) = default;
241#endif
242
243 _Empty_byte _M_empty;
244 _Up _M_value;
245 };
246
247#if __cpp_concepts < 202002L
248 template<typename _Up>
249 union _Storage<_Up, false>
250 {
251 constexpr _Storage() noexcept : _M_empty() { }
252
253 template<typename... _Args>
254 constexpr
255 _Storage(in_place_t, _Args&&... __args)
256 : _M_value(std::forward<_Args>(__args)...)
257 { }
258
259 template<typename _Vp, typename... _Args>
260 constexpr
261 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
262 : _M_value(__il, std::forward<_Args>(__args)...)
263 { }
264
265#if __cplusplus >= 202002L
266 template<typename _Fn, typename _Arg>
267 constexpr
268 _Storage(_Optional_func<_Fn> __f, _Arg&& __arg)
269 : _M_value(std::__invoke(std::forward<_Fn>(__f._M_f),
270 std::forward<_Arg>(__arg)))
271 { }
272#endif
273
274 // User-provided destructor is needed when _Up has non-trivial dtor.
275 _GLIBCXX20_CONSTEXPR ~_Storage() { }
276
277 _Storage(const _Storage&) = default;
278 _Storage(_Storage&&) = default;
279 _Storage& operator=(const _Storage&) = default;
280 _Storage& operator=(_Storage&&) = default;
281
282 _Empty_byte _M_empty;
283 _Up _M_value;
284 };
285#endif
286
287 _Storage<_Stored_type> _M_payload;
288
289 bool _M_engaged = false;
290
291 template<typename... _Args>
292 constexpr void
293 _M_construct(_Args&&... __args)
294 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
295 {
296 std::_Construct(std::__addressof(this->_M_payload._M_value),
297 std::forward<_Args>(__args)...);
298 this->_M_engaged = true;
299 }
300
301 constexpr void
302 _M_destroy() noexcept
303 {
304 _M_engaged = false;
305 _M_payload._M_value.~_Stored_type();
306 }
307
308#if __cplusplus >= 202002L
309 template<typename _Fn, typename _Up>
310 constexpr void
311 _M_apply(_Optional_func<_Fn> __f, _Up&& __x)
312 {
313 std::construct_at(std::__addressof(this->_M_payload),
314 __f, std::forward<_Up>(__x));
315 _M_engaged = true;
316 }
317#endif
318
319 // The _M_get() operations have _M_engaged as a precondition.
320 // They exist to access the contained value with the appropriate
321 // const-qualification, because _M_payload has had the const removed.
322
323 constexpr _Tp&
324 _M_get() noexcept
325 { return this->_M_payload._M_value; }
326
327 constexpr const _Tp&
328 _M_get() const noexcept
329 { return this->_M_payload._M_value; }
330
331 // _M_reset is a 'safe' operation with no precondition.
332 constexpr void
333 _M_reset() noexcept
334 {
335 if (this->_M_engaged)
336 _M_destroy();
337 else // This seems redundant but improves codegen, see PR 112480.
338 this->_M_engaged = false;
339 }
340 };
341
342 // Class template that manages the payload for optionals.
343 template <typename _Tp,
344 bool /*_HasTrivialDestructor*/ =
345 is_trivially_destructible_v<_Tp>,
346 bool /*_HasTrivialCopy */ =
347 is_trivially_copy_assignable_v<_Tp>
348 && is_trivially_copy_constructible_v<_Tp>,
349 bool /*_HasTrivialMove */ =
350 is_trivially_move_assignable_v<_Tp>
351 && is_trivially_move_constructible_v<_Tp>>
352 struct _Optional_payload;
353
354 // Payload for potentially-constexpr optionals (trivial copy/move/destroy).
355 template <typename _Tp>
356 struct _Optional_payload<_Tp, true, true, true>
357 : _Optional_payload_base<_Tp>
358 {
359 using _Optional_payload_base<_Tp>::_Optional_payload_base;
360
361 _Optional_payload() = default;
362 };
363
364 // Payload for optionals with non-trivial copy construction/assignment.
365 template <typename _Tp>
366 struct _Optional_payload<_Tp, true, false, true>
367 : _Optional_payload_base<_Tp>
368 {
369 using _Optional_payload_base<_Tp>::_Optional_payload_base;
370
371 _Optional_payload() = default;
372 ~_Optional_payload() = default;
373 _Optional_payload(const _Optional_payload&) = default;
374 _Optional_payload(_Optional_payload&&) = default;
375 _Optional_payload& operator=(_Optional_payload&&) = default;
376
377 // Non-trivial copy assignment.
378 constexpr
379 _Optional_payload&
380 operator=(const _Optional_payload& __other)
381 {
382 this->_M_copy_assign(__other);
383 return *this;
384 }
385 };
386
387 // Payload for optionals with non-trivial move construction/assignment.
388 template <typename _Tp>
389 struct _Optional_payload<_Tp, true, true, false>
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=(const _Optional_payload&) = default;
399
400 // Non-trivial move assignment.
401 constexpr
402 _Optional_payload&
403 operator=(_Optional_payload&& __other)
404 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
405 is_nothrow_move_assignable<_Tp>>)
406 {
407 this->_M_move_assign(std::move(__other));
408 return *this;
409 }
410 };
411
412 // Payload for optionals with non-trivial copy and move assignment.
413 template <typename _Tp>
414 struct _Optional_payload<_Tp, true, false, false>
415 : _Optional_payload_base<_Tp>
416 {
417 using _Optional_payload_base<_Tp>::_Optional_payload_base;
418
419 _Optional_payload() = default;
420 ~_Optional_payload() = default;
421 _Optional_payload(const _Optional_payload&) = default;
422 _Optional_payload(_Optional_payload&&) = default;
423
424 // Non-trivial copy assignment.
425 constexpr
426 _Optional_payload&
427 operator=(const _Optional_payload& __other)
428 {
429 this->_M_copy_assign(__other);
430 return *this;
431 }
432
433 // Non-trivial move assignment.
434 constexpr
435 _Optional_payload&
436 operator=(_Optional_payload&& __other)
437 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
438 is_nothrow_move_assignable<_Tp>>)
439 {
440 this->_M_move_assign(std::move(__other));
441 return *this;
442 }
443 };
444
445 // Payload for optionals with non-trivial destructors.
446 template <typename _Tp, bool _Copy, bool _Move>
447 struct _Optional_payload<_Tp, false, _Copy, _Move>
448 : _Optional_payload<_Tp, true, false, false>
449 {
450 // Base class implements all the constructors and assignment operators:
451 using _Optional_payload<_Tp, true, false, false>::_Optional_payload;
452 _Optional_payload() = default;
453 _Optional_payload(const _Optional_payload&) = default;
454 _Optional_payload(_Optional_payload&&) = default;
455 _Optional_payload& operator=(const _Optional_payload&) = default;
456 _Optional_payload& operator=(_Optional_payload&&) = default;
457
458 // Destructor needs to destroy the contained value:
459 _GLIBCXX20_CONSTEXPR ~_Optional_payload() { this->_M_reset(); }
460 };
461
462 /**
463 * @brief Class template that provides copy/move constructors of optional.
464 *
465 * Such a separate base class template is necessary in order to
466 * conditionally make copy/move constructors trivial.
467 *
468 * When the contained value is trivially copy/move constructible,
469 * the copy/move constructors of _Optional_base will invoke the
470 * trivial copy/move constructor of _Optional_payload. Otherwise,
471 * they will invoke _Optional_payload(bool, const _Optional_payload&)
472 * or _Optional_payload(bool, _Optional_payload&&) to initialize
473 * the contained value, if copying/moving an engaged optional.
474 *
475 * Whether the other special members are trivial is determined by the
476 * _Optional_payload<_Tp> specialization used for the _M_payload member.
477 *
478 * @see optional, _Enable_special_members
479 */
480 template<typename _Tp,
481 bool = is_trivially_copy_constructible_v<_Tp>,
482 bool = is_trivially_move_constructible_v<_Tp>>
483 struct _Optional_base
484 {
485 // Constructors for disengaged optionals.
486 constexpr _Optional_base() = default;
487
488 // Constructors for engaged optionals.
489 template<typename... _Args,
490 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
491 constexpr explicit
492 _Optional_base(in_place_t, _Args&&... __args)
493 : _M_payload(in_place, std::forward<_Args>(__args)...)
494 { }
495
496 template<typename _Up, typename... _Args,
497 enable_if_t<is_constructible_v<_Tp,
498 initializer_list<_Up>&,
499 _Args...>, bool> = false>
500 constexpr explicit
501 _Optional_base(in_place_t,
502 initializer_list<_Up> __il,
503 _Args&&... __args)
504 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
505 { }
506
507 // Copy and move constructors.
508 constexpr
509 _Optional_base(const _Optional_base& __other)
510 noexcept(is_nothrow_copy_constructible_v<_Tp>)
511 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
512 { }
513
514 constexpr
515 _Optional_base(_Optional_base&& __other)
516 noexcept(is_nothrow_move_constructible_v<_Tp>)
517 : _M_payload(__other._M_payload._M_engaged,
518 std::move(__other._M_payload))
519 { }
520
521#if __cpp_concepts >= 202002L // Conditionally trivial special member functions
522 // Define these in the primary template if possible, so that we don't
523 // need to use partial specializations of this class template.
524 constexpr _Optional_base(const _Optional_base&)
525 requires is_trivially_copy_constructible_v<_Tp> = default;
526
527 constexpr _Optional_base(_Optional_base&&)
528 requires is_trivially_move_constructible_v<_Tp> = default;
529#endif
530
531 // Assignment operators.
532 _Optional_base& operator=(const _Optional_base&) = default;
533 _Optional_base& operator=(_Optional_base&&) = default;
534
535 _Optional_payload<_Tp> _M_payload;
536
537 protected:
538 // For the primary template, we define these functions here.
539 using _Stored_type = remove_const_t<_Tp>;
540
541 // The _M_construct operation has !_M_engaged as a precondition
542 // while _M_destruct has _M_engaged as a precondition.
543 template<typename... _Args>
544 constexpr void
545 _M_construct(_Args&&... __args)
546 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
547 {
548 _M_payload._M_construct(std::forward<_Args>(__args)...);
549 }
550
551 constexpr void
552 _M_destruct() noexcept
553 { _M_payload._M_destroy(); }
554
555 // _M_reset is a 'safe' operation with no precondition.
556 constexpr void
557 _M_reset() noexcept
558 { _M_payload._M_reset(); }
559
560 constexpr bool _M_is_engaged() const noexcept
561 { return _M_payload._M_engaged; }
562
563 // The _M_get operations have _M_engaged as a precondition.
564 constexpr _Tp&
565 _M_get() noexcept
566 { return _M_payload._M_get(); }
567
568 constexpr const _Tp&
569 _M_get() const noexcept
570 { return _M_payload._M_get(); }
571 };
572
573#if __cpp_concepts < 202002L
574 // If P0848R3 "Conditionally Trivial Special Member Functions" is not
575 // supported (as determined from the __cpp_concepts macro value), the
576 // _Optional_base primary template only has non-trivial copy and move
577 // constructors. Use partial specializations of _Optional_base<T, C, M>
578 // that have a trivial copy and/or move constructor.
579
580 // Common base class for _Optional_base<T> to avoid repeating these
581 // member functions in each partial specialization.
582 // Only used if P0848R3 "Conditionally Trivial Special Member Functions"
583 // is not supported, as indicated by the __cpp_concepts value.
584 template<typename _Tp, typename _Dp>
585 class _Optional_base_impl
586 {
587 protected:
588 using _Stored_type = remove_const_t<_Tp>;
589
590 // The _M_construct operation has !_M_engaged as a precondition
591 // while _M_destruct has _M_engaged as a precondition.
592 template<typename... _Args>
593 constexpr void
594 _M_construct(_Args&&... __args)
595 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
596 {
597 static_cast<_Dp*>(this)->_M_payload._M_construct(
598 std::forward<_Args>(__args)...);
599 }
600
601 constexpr void
602 _M_destruct() noexcept
603 { static_cast<_Dp*>(this)->_M_payload._M_destroy(); }
604
605 // _M_reset is a 'safe' operation with no precondition.
606 constexpr void
607 _M_reset() noexcept
608 { static_cast<_Dp*>(this)->_M_payload._M_reset(); }
609
610 constexpr bool _M_is_engaged() const noexcept
611 { return static_cast<const _Dp*>(this)->_M_payload._M_engaged; }
612
613 // The _M_get operations have _M_engaged as a precondition.
614 constexpr _Tp&
615 _M_get() noexcept
616 { return static_cast<_Dp*>(this)->_M_payload._M_get(); }
617
618 constexpr const _Tp&
619 _M_get() const noexcept
620 { return static_cast<const _Dp*>(this)->_M_payload._M_get(); }
621 };
622
623 template<typename _Tp>
624 struct _Optional_base<_Tp, false, true> // trivial move ctor
625 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
626 {
627 // Constructors for disengaged optionals.
628 constexpr _Optional_base() = default;
629
630 // Constructors for engaged optionals.
631 template<typename... _Args,
632 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
633 constexpr explicit
634 _Optional_base(in_place_t, _Args&&... __args)
635 : _M_payload(in_place, std::forward<_Args>(__args)...)
636 { }
637
638 template<typename _Up, typename... _Args,
639 enable_if_t<is_constructible_v<_Tp,
640 initializer_list<_Up>&,
641 _Args...>, bool> = false>
642 constexpr explicit
643 _Optional_base(in_place_t,
644 initializer_list<_Up> __il,
645 _Args... __args)
646 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
647 { }
648
649 // Copy and move constructors.
650 constexpr _Optional_base(const _Optional_base& __other)
651 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
652 { }
653
654 constexpr _Optional_base(_Optional_base&& __other) = default;
655
656 // Assignment operators.
657 _Optional_base& operator=(const _Optional_base&) = default;
658 _Optional_base& operator=(_Optional_base&&) = default;
659
660 _Optional_payload<_Tp> _M_payload;
661 };
662
663 template<typename _Tp>
664 struct _Optional_base<_Tp, true, false> // trivial copy ctor
665 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
666 {
667 // Constructors for disengaged optionals.
668 constexpr _Optional_base() = default;
669
670 // Constructors for engaged optionals.
671 template<typename... _Args,
672 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
673 constexpr explicit
674 _Optional_base(in_place_t, _Args&&... __args)
675 : _M_payload(in_place, std::forward<_Args>(__args)...)
676 { }
677
678 template<typename _Up, typename... _Args,
679 enable_if_t<is_constructible_v<_Tp,
680 initializer_list<_Up>&,
681 _Args...>, bool> = false>
682 constexpr explicit
683 _Optional_base(in_place_t,
684 initializer_list<_Up> __il,
685 _Args&&... __args)
686 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
687 { }
688
689 // Copy and move constructors.
690 constexpr _Optional_base(const _Optional_base& __other) = default;
691
692 constexpr
693 _Optional_base(_Optional_base&& __other)
694 noexcept(is_nothrow_move_constructible_v<_Tp>)
695 : _M_payload(__other._M_payload._M_engaged,
696 std::move(__other._M_payload))
697 { }
698
699 // Assignment operators.
700 _Optional_base& operator=(const _Optional_base&) = default;
701 _Optional_base& operator=(_Optional_base&&) = default;
702
703 _Optional_payload<_Tp> _M_payload;
704 };
705
706 template<typename _Tp>
707 struct _Optional_base<_Tp, true, true> // trivial copy and move ctors
708 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
709 {
710 // Constructors for disengaged optionals.
711 constexpr _Optional_base() = default;
712
713 // Constructors for engaged optionals.
714 template<typename... _Args,
715 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
716 constexpr explicit
717 _Optional_base(in_place_t, _Args&&... __args)
718 : _M_payload(in_place, std::forward<_Args>(__args)...)
719 { }
720
721 template<typename _Up, typename... _Args,
722 enable_if_t<is_constructible_v<_Tp,
723 initializer_list<_Up>&,
724 _Args...>, bool> = false>
725 constexpr explicit
726 _Optional_base(in_place_t,
727 initializer_list<_Up> __il,
728 _Args&&... __args)
729 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
730 { }
731
732 // Copy and move constructors.
733 constexpr _Optional_base(const _Optional_base& __other) = default;
734 constexpr _Optional_base(_Optional_base&& __other) = default;
735
736 // Assignment operators.
737 _Optional_base& operator=(const _Optional_base&) = default;
738 _Optional_base& operator=(_Optional_base&&) = default;
739
740 _Optional_payload<_Tp> _M_payload;
741 };
742#endif // __cpp_concepts
743
744 template<typename _Tp>
745 inline constexpr bool __is_optional_v = false;
746 template<typename _Tp>
747 inline constexpr bool __is_optional_v<optional<_Tp>> = true;
748
749 template<typename _Tp, typename _Wp>
750 using __converts_from_any_cvref = __or_<
751 is_constructible<_Tp, _Wp&>, is_convertible<_Wp&, _Tp>,
752 is_constructible<_Tp, _Wp>, is_convertible<_Wp, _Tp>,
753 is_constructible<_Tp, const _Wp&>, is_convertible<const _Wp&, _Tp>,
754 is_constructible<_Tp, const _Wp>, is_convertible<const _Wp, _Tp>
755 >;
756
757 template<typename _Tp, typename _Up>
758 using __converts_from_optional
759 = __converts_from_any_cvref<_Tp, optional<_Up>>;
760
761 template<typename _Tp, typename _Up>
762 using __assigns_from_optional =
763 __or_<is_assignable<_Tp&, const optional<_Up>&>,
764 is_assignable<_Tp&, optional<_Up>&>,
765 is_assignable<_Tp&, const optional<_Up>&&>,
766 is_assignable<_Tp&, optional<_Up>&&>>;
767
768#if __cpp_concepts && __cpp_conditional_explicit && __glibcxx_remove_cvref
769# define _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL 1
770#endif
771
772 /**
773 * @brief Class template for optional values.
774 */
775 template<typename _Tp>
776 class optional
777 : private _Optional_base<_Tp>,
778 private _Enable_copy_move<
779 // Copy constructor.
780 is_copy_constructible_v<_Tp>,
781 // Copy assignment.
782 __and_v<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>,
783 // Move constructor.
784 is_move_constructible_v<_Tp>,
785 // Move assignment.
786 __and_v<is_move_constructible<_Tp>, is_move_assignable<_Tp>>,
787 // Unique tag type.
788 optional<_Tp>>
789 {
790 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
791 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
792 static_assert(is_object_v<_Tp> && !is_array_v<_Tp>);
793
794 private:
795 using _Base = _Optional_base<_Tp>;
796
797 // SFINAE helpers
798
799 // _GLIBCXX_RESOLVE_LIB_DEFECTS
800 // 3836. std::expected<bool, E1> conversion constructor
801 // expected(const expected<U, G>&) should take precedence over
802 // expected(U&&) with operator bool
803#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
804 template<typename _From, typename = remove_cv_t<_Tp>>
805 static constexpr bool __not_constructing_bool_from_optional
806 = true;
807
808 // If T is cv bool, remove_cvref_t<U> is not a specialization of optional
809 // i.e. do not initialize a bool from optional<U>::operator bool().
810 template<typename _From>
811 static constexpr bool
812 __not_constructing_bool_from_optional<_From, bool>
813 = !__is_optional_v<remove_cvref_t<_From>>;
814
815 // If T is not cv bool, converts-from-any-cvref<T, optional<U>> is false.
816 // The constructor that converts from optional<U> is disabled if the
817 // contained value can be initialized from optional<U>, so that the
818 // optional(U&&) constructor can be used instead.
819 template<typename _From, typename = remove_cv_t<_Tp>>
820 static constexpr bool __construct_from_contained_value
821 = !__converts_from_optional<_Tp, _From>::value;
822
823 // However, optional<U> can always be converted to bool, so don't apply
824 // this constraint when T is cv bool.
825 template<typename _From>
826 static constexpr bool __construct_from_contained_value<_From, bool>
827 = true;
828#else
829 template<typename _From, typename = remove_cv_t<_Tp>>
830 struct __not_constructing_bool_from_optional
831 : true_type
832 { };
833
834 template<typename _From>
835 struct __not_constructing_bool_from_optional<_From, bool>
836 : bool_constant<!__is_optional_v<__remove_cvref_t<_From>>>
837 { };
838
839 template<typename _From, typename = remove_cv_t<_Tp>>
840 struct __construct_from_contained_value
841 : __not_<__converts_from_optional<_Tp, _From>>
842 { };
843
844 template<typename _From>
845 struct __construct_from_contained_value<_From, bool>
846 : true_type
847 { };
848
849 template<typename _Up>
850 using __not_self = __not_<is_same<optional, __remove_cvref_t<_Up>>>;
851 template<typename _Up>
852 using __not_tag = __not_<is_same<in_place_t, __remove_cvref_t<_Up>>>;
853 template<typename _Up>
854 using __is_bool = is_same<remove_cv_t<_Up>, bool>;
855 template<typename... _Cond>
856 using _Requires = enable_if_t<__and_v<_Cond...>, bool>;
857#endif
858
859 public:
860 using value_type = _Tp;
861
862 constexpr optional() noexcept { }
863
864 constexpr optional(nullopt_t) noexcept { }
865
866 // Converting constructors for engaged optionals.
867#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
868 template<typename _Up = remove_cv_t<_Tp>>
869 requires (!is_same_v<optional, remove_cvref_t<_Up>>)
870 && (!is_same_v<in_place_t, remove_cvref_t<_Up>>)
871 && is_constructible_v<_Tp, _Up>
872 && __not_constructing_bool_from_optional<_Up>
873 constexpr explicit(!is_convertible_v<_Up, _Tp>)
874 optional(_Up&& __t)
875 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
876 : _Base(std::in_place, std::forward<_Up>(__t)) { }
877
878 template<typename _Up>
879 requires (!is_same_v<optional, remove_cvref_t<_Up>>)
880 && is_constructible_v<_Tp, const _Up&>
881 && __construct_from_contained_value<_Up>
882 constexpr explicit(!is_convertible_v<const _Up&, _Tp>)
883 optional(const optional<_Up>& __t)
884 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
885 {
886 if (__t)
887 emplace(__t._M_get());
888 }
889
890 template<typename _Up>
891 requires (!is_same_v<optional, remove_cvref_t<_Up>>)
892 && is_constructible_v<_Tp, _Up>
893 && __construct_from_contained_value<_Up>
894 constexpr explicit(!is_convertible_v<_Up, _Tp>)
895 optional(optional<_Up>&& __t)
896 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
897 {
898 if (__t)
899 emplace(std::move(__t._M_get()));
900 }
901
902 template<typename... _Args>
903 requires is_constructible_v<_Tp, _Args...>
904 explicit constexpr
905 optional(in_place_t, _Args&&... __args)
906 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
907 : _Base(std::in_place, std::forward<_Args>(__args)...)
908 { }
909
910 template<typename _Up, typename... _Args>
911 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
912 explicit constexpr
913 optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
914 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
915 _Args...>)
916 : _Base(std::in_place, __il, std::forward<_Args>(__args)...)
917 { }
918#else
919 template<typename _Up = remove_cv_t<_Tp>,
920 _Requires<__not_self<_Up>, __not_tag<_Up>,
921 is_constructible<_Tp, _Up>,
922 is_convertible<_Up, _Tp>,
923 __not_constructing_bool_from_optional<_Up>> = true>
924 constexpr
925 optional(_Up&& __t)
926 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
927 : _Base(std::in_place, std::forward<_Up>(__t)) { }
928
929 template<typename _Up = remove_cv_t<_Tp>,
930 _Requires<__not_self<_Up>, __not_tag<_Up>,
931 is_constructible<_Tp, _Up>,
932 __not_<is_convertible<_Up, _Tp>>,
933 __not_constructing_bool_from_optional<_Up>> = false>
934 explicit constexpr
935 optional(_Up&& __t)
936 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
937 : _Base(std::in_place, std::forward<_Up>(__t)) { }
938
939 template<typename _Up,
940 _Requires<__not_<is_same<_Tp, _Up>>,
941 is_constructible<_Tp, const _Up&>,
942 is_convertible<const _Up&, _Tp>,
943 __construct_from_contained_value<_Up>> = true>
944 constexpr
945 optional(const optional<_Up>& __t)
946 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
947 {
948 if (__t)
949 emplace(__t._M_get());
950 }
951
952 template<typename _Up,
953 _Requires<__not_<is_same<_Tp, _Up>>,
954 is_constructible<_Tp, const _Up&>,
955 __not_<is_convertible<const _Up&, _Tp>>,
956 __construct_from_contained_value<_Up>> = false>
957 explicit constexpr
958 optional(const optional<_Up>& __t)
959 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
960 {
961 if (__t)
962 emplace(__t._M_get());
963 }
964
965 template<typename _Up,
966 _Requires<__not_<is_same<_Tp, _Up>>,
967 is_constructible<_Tp, _Up>,
968 is_convertible<_Up, _Tp>,
969 __construct_from_contained_value<_Up>> = true>
970 constexpr
971 optional(optional<_Up>&& __t)
972 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
973 {
974 if (__t)
975 emplace(std::move(__t._M_get()));
976 }
977
978 template<typename _Up,
979 _Requires<__not_<is_same<_Tp, _Up>>,
980 is_constructible<_Tp, _Up>,
981 __not_<is_convertible<_Up, _Tp>>,
982 __construct_from_contained_value<_Up>> = false>
983 explicit constexpr
984 optional(optional<_Up>&& __t)
985 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
986 {
987 if (__t)
988 emplace(std::move(__t._M_get()));
989 }
990
991 template<typename... _Args,
992 _Requires<is_constructible<_Tp, _Args...>> = false>
993 explicit constexpr
994 optional(in_place_t, _Args&&... __args)
995 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
996 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
997
998 template<typename _Up, typename... _Args,
999 _Requires<is_constructible<_Tp,
1000 initializer_list<_Up>&,
1001 _Args...>> = false>
1002 explicit constexpr
1003 optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
1004 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
1005 _Args...>)
1006 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
1007#endif
1008
1009 // Assignment operators.
1010 _GLIBCXX20_CONSTEXPR optional&
1011 operator=(nullopt_t) noexcept
1012 {
1013 this->_M_reset();
1014 return *this;
1015 }
1016
1017 template<typename _Up = remove_cv_t<_Tp>>
1018#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
1019 requires (!is_same_v<optional, remove_cvref_t<_Up>>)
1020 && (!(is_scalar_v<_Tp> && is_same_v<_Tp, decay_t<_Up>>))
1021 && is_constructible_v<_Tp, _Up>
1022 && is_assignable_v<_Tp&, _Up>
1023 constexpr optional&
1024#else
1025 enable_if_t<__and_v<__not_self<_Up>,
1026 __not_<__and_<is_scalar<_Tp>,
1027 is_same<_Tp, decay_t<_Up>>>>,
1028 is_constructible<_Tp, _Up>,
1029 is_assignable<_Tp&, _Up>>,
1030 optional&>
1031#endif
1032 operator=(_Up&& __u)
1033 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
1034 is_nothrow_assignable<_Tp&, _Up>>)
1035 {
1036 if (this->_M_is_engaged())
1037 this->_M_get() = std::forward<_Up>(__u);
1038 else
1039 this->_M_construct(std::forward<_Up>(__u));
1040
1041 return *this;
1042 }
1043
1044 template<typename _Up>
1045#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
1046 requires (!is_same_v<optional, remove_cvref_t<_Up>>)
1047 && is_constructible_v<_Tp, const _Up&>
1048 && is_assignable_v<_Tp&, const _Up&>
1049 && (!__converts_from_optional<_Tp, _Up>::value)
1050 && (!__assigns_from_optional<_Tp, _Up>::value)
1051 constexpr optional&
1052#else
1053 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
1054 is_constructible<_Tp, const _Up&>,
1055 is_assignable<_Tp&, const _Up&>,
1056 __not_<__converts_from_optional<_Tp, _Up>>,
1057 __not_<__assigns_from_optional<_Tp, _Up>>>,
1058 optional&>
1059#endif
1060 operator=(const optional<_Up>& __u)
1061 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
1062 is_nothrow_assignable<_Tp&, const _Up&>>)
1063 {
1064 if (__u)
1065 {
1066 if (this->_M_is_engaged())
1067 this->_M_get() = __u._M_get();
1068 else
1069 this->_M_construct(__u._M_get());
1070 }
1071 else
1072 {
1073 this->_M_reset();
1074 }
1075 return *this;
1076 }
1077
1078 template<typename _Up>
1079#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
1080 requires (!is_same_v<optional, remove_cvref_t<_Up>>)
1081 && is_constructible_v<_Tp, _Up>
1082 && is_assignable_v<_Tp&, _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, _Up>,
1089 is_assignable<_Tp&, _Up>,
1090 __not_<__converts_from_optional<_Tp, _Up>>,
1091 __not_<__assigns_from_optional<_Tp, _Up>>>,
1092 optional&>
1093#endif
1094 operator=(optional<_Up>&& __u)
1095 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
1096 is_nothrow_assignable<_Tp&, _Up>>)
1097 {
1098 if (__u)
1099 {
1100 if (this->_M_is_engaged())
1101 this->_M_get() = std::move(__u._M_get());
1102 else
1103 this->_M_construct(std::move(__u._M_get()));
1104 }
1105 else
1106 {
1107 this->_M_reset();
1108 }
1109
1110 return *this;
1111 }
1112
1113 template<typename... _Args>
1114 _GLIBCXX20_CONSTEXPR
1115 enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
1116 emplace(_Args&&... __args)
1117 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1118 {
1119 this->_M_reset();
1120 this->_M_construct(std::forward<_Args>(__args)...);
1121 return this->_M_get();
1122 }
1123
1124 template<typename _Up, typename... _Args>
1125 _GLIBCXX20_CONSTEXPR
1126 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1127 _Tp&>
1128 emplace(initializer_list<_Up> __il, _Args&&... __args)
1129 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
1130 _Args...>)
1131 {
1132 this->_M_reset();
1133 this->_M_construct(__il, std::forward<_Args>(__args)...);
1134 return this->_M_get();
1135 }
1136
1137 // Destructor is implicit, implemented in _Optional_base.
1138
1139 // Swap.
1140 _GLIBCXX20_CONSTEXPR void
1141 swap(optional& __other)
1142 noexcept(is_nothrow_move_constructible_v<_Tp>
1143 && is_nothrow_swappable_v<_Tp>)
1144 {
1145 using std::swap;
1146
1147 if (this->_M_is_engaged() && __other._M_is_engaged())
1148 swap(this->_M_get(), __other._M_get());
1149 else if (this->_M_is_engaged())
1150 {
1151 __other._M_construct(std::move(this->_M_get()));
1152 this->_M_destruct();
1153 }
1154 else if (__other._M_is_engaged())
1155 {
1156 this->_M_construct(std::move(__other._M_get()));
1157 __other._M_destruct();
1158 }
1159 }
1160
1161 // Observers.
1162 constexpr const _Tp*
1163 operator->() const noexcept
1164 {
1165 __glibcxx_assert(this->_M_is_engaged());
1166 return std::__addressof(this->_M_get());
1167 }
1168
1169 constexpr _Tp*
1170 operator->() noexcept
1171 {
1172 __glibcxx_assert(this->_M_is_engaged());
1173 return std::__addressof(this->_M_get());
1174 }
1175
1176 constexpr const _Tp&
1177 operator*() const& noexcept
1178 {
1179 __glibcxx_assert(this->_M_is_engaged());
1180 return this->_M_get();
1181 }
1182
1183 constexpr _Tp&
1184 operator*()& noexcept
1185 {
1186 __glibcxx_assert(this->_M_is_engaged());
1187 return this->_M_get();
1188 }
1189
1190 constexpr _Tp&&
1191 operator*()&& noexcept
1192 {
1193 __glibcxx_assert(this->_M_is_engaged());
1194 return std::move(this->_M_get());
1195 }
1196
1197 constexpr const _Tp&&
1198 operator*() const&& noexcept
1199 {
1200 __glibcxx_assert(this->_M_is_engaged());
1201 return std::move(this->_M_get());
1202 }
1203
1204 constexpr explicit operator bool() const noexcept
1205 { return this->_M_is_engaged(); }
1206
1207 constexpr bool has_value() const noexcept
1208 { return this->_M_is_engaged(); }
1209
1210 constexpr const _Tp&
1211 value() const&
1212 {
1213 if (this->_M_is_engaged())
1214 return this->_M_get();
1215 __throw_bad_optional_access();
1216 }
1217
1218 constexpr _Tp&
1219 value()&
1220 {
1221 if (this->_M_is_engaged())
1222 return this->_M_get();
1223 __throw_bad_optional_access();
1224 }
1225
1226 constexpr _Tp&&
1227 value()&&
1228 {
1229 if (this->_M_is_engaged())
1230 return std::move(this->_M_get());
1231 __throw_bad_optional_access();
1232 }
1233
1234 constexpr const _Tp&&
1235 value() const&&
1236 {
1237 if (this->_M_is_engaged())
1238 return std::move(this->_M_get());
1239 __throw_bad_optional_access();
1240 }
1241
1242 template<typename _Up = remove_cv_t<_Tp>>
1243 constexpr _Tp
1244 value_or(_Up&& __u) const&
1245 {
1246 static_assert(is_copy_constructible_v<_Tp>);
1247 static_assert(is_convertible_v<_Up&&, _Tp>);
1248
1249 if (this->_M_is_engaged())
1250 return this->_M_get();
1251 else
1252 return static_cast<_Tp>(std::forward<_Up>(__u));
1253 }
1254
1255 template<typename _Up = remove_cv_t<_Tp>>
1256 constexpr _Tp
1257 value_or(_Up&& __u) &&
1258 {
1259 static_assert(is_move_constructible_v<_Tp>);
1260 static_assert(is_convertible_v<_Up&&, _Tp>);
1261
1262 if (this->_M_is_engaged())
1263 return std::move(this->_M_get());
1264 else
1265 return static_cast<_Tp>(std::forward<_Up>(__u));
1266 }
1267
1268#if __cpp_lib_optional >= 202110L // C++23
1269 // [optional.monadic]
1270
1271 template<typename _Fn>
1272 constexpr auto
1273 and_then(_Fn&& __f) &
1274 {
1275 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp&>>;
1276 static_assert(__is_optional_v<remove_cvref_t<_Up>>,
1277 "the function passed to std::optional<T>::and_then "
1278 "must return a std::optional");
1279 if (has_value())
1280 return std::__invoke(std::forward<_Fn>(__f), _M_get());
1281 else
1282 return _Up();
1283 }
1284
1285 template<typename _Fn>
1286 constexpr auto
1287 and_then(_Fn&& __f) const &
1288 {
1289 using _Up = remove_cvref_t<invoke_result_t<_Fn, const _Tp&>>;
1290 static_assert(__is_optional_v<_Up>,
1291 "the function passed to std::optional<T>::and_then "
1292 "must return a std::optional");
1293 if (has_value())
1294 return std::__invoke(std::forward<_Fn>(__f), _M_get());
1295 else
1296 return _Up();
1297 }
1298
1299 template<typename _Fn>
1300 constexpr auto
1301 and_then(_Fn&& __f) &&
1302 {
1303 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp>>;
1304 static_assert(__is_optional_v<remove_cvref_t<_Up>>,
1305 "the function passed to std::optional<T>::and_then "
1306 "must return a std::optional");
1307 if (has_value())
1308 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_get()));
1309 else
1310 return _Up();
1311 }
1312
1313 template<typename _Fn>
1314 constexpr auto
1315 and_then(_Fn&& __f) const &&
1316 {
1317 using _Up = remove_cvref_t<invoke_result_t<_Fn, const _Tp>>;
1318 static_assert(__is_optional_v<remove_cvref_t<_Up>>,
1319 "the function passed to std::optional<T>::and_then "
1320 "must return a std::optional");
1321 if (has_value())
1322 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_get()));
1323 else
1324 return _Up();
1325 }
1326
1327 template<typename _Fn>
1328 constexpr auto
1329 transform(_Fn&& __f) &
1330 {
1331 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp&>>;
1332 if (has_value())
1333 return optional<_Up>(_Optional_func<_Fn>{__f}, _M_get());
1334 else
1335 return optional<_Up>();
1336 }
1337
1338 template<typename _Fn>
1339 constexpr auto
1340 transform(_Fn&& __f) const &
1341 {
1342 using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp&>>;
1343 if (has_value())
1344 return optional<_Up>(_Optional_func<_Fn>{__f}, _M_get());
1345 else
1346 return optional<_Up>();
1347 }
1348
1349 template<typename _Fn>
1350 constexpr auto
1351 transform(_Fn&& __f) &&
1352 {
1353 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp>>;
1354 if (has_value())
1355 return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(_M_get()));
1356 else
1357 return optional<_Up>();
1358 }
1359
1360 template<typename _Fn>
1361 constexpr auto
1362 transform(_Fn&& __f) const &&
1363 {
1364 using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp>>;
1365 if (has_value())
1366 return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(_M_get()));
1367 else
1368 return optional<_Up>();
1369 }
1370
1371 template<typename _Fn> requires invocable<_Fn> && copy_constructible<_Tp>
1372 constexpr optional
1373 or_else(_Fn&& __f) const&
1374 {
1375 using _Up = invoke_result_t<_Fn>;
1376 static_assert(is_same_v<remove_cvref_t<_Up>, optional>,
1377 "the function passed to std::optional<T>::or_else "
1378 "must return a std::optional<T>");
1379
1380 if (has_value())
1381 return *this;
1382 else
1383 return std::forward<_Fn>(__f)();
1384 }
1385
1386 template<typename _Fn> requires invocable<_Fn> && move_constructible<_Tp>
1387 constexpr optional
1388 or_else(_Fn&& __f) &&
1389 {
1390 using _Up = invoke_result_t<_Fn>;
1391 static_assert(is_same_v<remove_cvref_t<_Up>, optional>,
1392 "the function passed to std::optional<T>::or_else "
1393 "must return a std::optional<T>");
1394
1395 if (has_value())
1396 return std::move(*this);
1397 else
1398 return std::forward<_Fn>(__f)();
1399 }
1400#endif
1401
1402 _GLIBCXX20_CONSTEXPR void reset() noexcept { this->_M_reset(); }
1403
1404 private:
1405 using _Base::_M_get;
1406
1407 template<typename _Up> friend class optional;
1408
1409#if __cpp_lib_optional >= 202110L // C++23
1410 template<typename _Fn, typename _Value>
1411 explicit constexpr
1412 optional(_Optional_func<_Fn> __f, _Value&& __v)
1413 {
1414 this->_M_payload._M_apply(__f, std::forward<_Value>(__v));
1415 }
1416#endif
1417 };
1418
1419 template<typename _Tp>
1420 using __optional_relop_t =
1421 enable_if_t<is_convertible_v<_Tp, bool>, bool>;
1422
1423 template<typename _Tp, typename _Up>
1424 using __optional_eq_t = __optional_relop_t<
1425 decltype(std::declval<const _Tp&>() == std::declval<const _Up&>())
1426 >;
1427
1428 template<typename _Tp, typename _Up>
1429 using __optional_ne_t = __optional_relop_t<
1430 decltype(std::declval<const _Tp&>() != std::declval<const _Up&>())
1431 >;
1432
1433 template<typename _Tp, typename _Up>
1434 using __optional_lt_t = __optional_relop_t<
1435 decltype(std::declval<const _Tp&>() < std::declval<const _Up&>())
1436 >;
1437
1438 template<typename _Tp, typename _Up>
1439 using __optional_gt_t = __optional_relop_t<
1440 decltype(std::declval<const _Tp&>() > std::declval<const _Up&>())
1441 >;
1442
1443 template<typename _Tp, typename _Up>
1444 using __optional_le_t = __optional_relop_t<
1445 decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>())
1446 >;
1447
1448 template<typename _Tp, typename _Up>
1449 using __optional_ge_t = __optional_relop_t<
1450 decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>())
1451 >;
1452
1453 // Comparisons between optional values.
1454 template<typename _Tp, typename _Up>
1455 constexpr auto
1456 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1457 -> __optional_eq_t<_Tp, _Up>
1458 {
1459 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
1460 && (!__lhs || *__lhs == *__rhs);
1461 }
1462
1463 template<typename _Tp, typename _Up>
1464 constexpr auto
1465 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1466 -> __optional_ne_t<_Tp, _Up>
1467 {
1468 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
1469 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
1470 }
1471
1472 template<typename _Tp, typename _Up>
1473 constexpr auto
1474 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1475 -> __optional_lt_t<_Tp, _Up>
1476 {
1477 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
1478 }
1479
1480 template<typename _Tp, typename _Up>
1481 constexpr auto
1482 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1483 -> __optional_gt_t<_Tp, _Up>
1484 {
1485 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
1486 }
1487
1488 template<typename _Tp, typename _Up>
1489 constexpr auto
1490 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1491 -> __optional_le_t<_Tp, _Up>
1492 {
1493 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
1494 }
1495
1496 template<typename _Tp, typename _Up>
1497 constexpr auto
1498 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1499 -> __optional_ge_t<_Tp, _Up>
1500 {
1501 return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
1502 }
1503
1504#ifdef __cpp_lib_three_way_comparison
1505 template<typename _Tp, three_way_comparable_with<_Tp> _Up>
1506 [[nodiscard]]
1507 constexpr compare_three_way_result_t<_Tp, _Up>
1508 operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
1509 {
1510 return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
1511 }
1512#endif
1513
1514 // Comparisons with nullopt.
1515 template<typename _Tp>
1516 [[nodiscard]]
1517 constexpr bool
1518 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
1519 { return !__lhs; }
1520
1521#ifdef __cpp_lib_three_way_comparison
1522 template<typename _Tp>
1523 [[nodiscard]]
1524 constexpr strong_ordering
1525 operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
1526 { return bool(__x) <=> false; }
1527#else
1528 template<typename _Tp>
1529 constexpr bool
1530 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
1531 { return !__rhs; }
1532
1533 template<typename _Tp>
1534 constexpr bool
1535 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1536 { return static_cast<bool>(__lhs); }
1537
1538 template<typename _Tp>
1539 constexpr bool
1540 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1541 { return static_cast<bool>(__rhs); }
1542
1543 template<typename _Tp>
1544 constexpr bool
1545 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1546 { return false; }
1547
1548 template<typename _Tp>
1549 constexpr bool
1550 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
1551 { return static_cast<bool>(__rhs); }
1552
1553 template<typename _Tp>
1554 constexpr bool
1555 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
1556 { return static_cast<bool>(__lhs); }
1557
1558 template<typename _Tp>
1559 constexpr bool
1560 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1561 { return false; }
1562
1563 template<typename _Tp>
1564 constexpr bool
1565 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1566 { return !__lhs; }
1567
1568 template<typename _Tp>
1569 constexpr bool
1570 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1571 { return true; }
1572
1573 template<typename _Tp>
1574 constexpr bool
1575 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1576 { return true; }
1577
1578 template<typename _Tp>
1579 constexpr bool
1580 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1581 { return !__rhs; }
1582#endif // three-way-comparison
1583
1584#if __cpp_lib_concepts
1585 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1586 // 4072. std::optional comparisons: constrain harder
1587# define _REQUIRES_NOT_OPTIONAL(T) requires (!__is_optional_v<T>)
1588#else
1589# define _REQUIRES_NOT_OPTIONAL(T)
1590#endif
1591
1592 // Comparisons with value type.
1593 template<typename _Tp, typename _Up>
1594 _REQUIRES_NOT_OPTIONAL(_Up)
1595 constexpr auto
1596 operator== [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
1597 -> __optional_eq_t<_Tp, _Up>
1598 { return __lhs && *__lhs == __rhs; }
1599
1600 template<typename _Tp, typename _Up>
1601 _REQUIRES_NOT_OPTIONAL(_Tp)
1602 constexpr auto
1603 operator== [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
1604 -> __optional_eq_t<_Tp, _Up>
1605 { return __rhs && __lhs == *__rhs; }
1606
1607 template<typename _Tp, typename _Up>
1608 _REQUIRES_NOT_OPTIONAL(_Up)
1609 constexpr auto
1610 operator!= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
1611 -> __optional_ne_t<_Tp, _Up>
1612 { return !__lhs || *__lhs != __rhs; }
1613
1614 template<typename _Tp, typename _Up>
1615 _REQUIRES_NOT_OPTIONAL(_Tp)
1616 constexpr auto
1617 operator!= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
1618 -> __optional_ne_t<_Tp, _Up>
1619 { return !__rhs || __lhs != *__rhs; }
1620
1621 template<typename _Tp, typename _Up>
1622 _REQUIRES_NOT_OPTIONAL(_Up)
1623 constexpr auto
1624 operator< [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
1625 -> __optional_lt_t<_Tp, _Up>
1626 { return !__lhs || *__lhs < __rhs; }
1627
1628 template<typename _Tp, typename _Up>
1629 _REQUIRES_NOT_OPTIONAL(_Tp)
1630 constexpr auto
1631 operator< [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
1632 -> __optional_lt_t<_Tp, _Up>
1633 { return __rhs && __lhs < *__rhs; }
1634
1635 template<typename _Tp, typename _Up>
1636 _REQUIRES_NOT_OPTIONAL(_Up)
1637 constexpr auto
1638 operator> [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
1639 -> __optional_gt_t<_Tp, _Up>
1640 { return __lhs && *__lhs > __rhs; }
1641
1642 template<typename _Tp, typename _Up>
1643 _REQUIRES_NOT_OPTIONAL(_Tp)
1644 constexpr auto
1645 operator> [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
1646 -> __optional_gt_t<_Tp, _Up>
1647 { return !__rhs || __lhs > *__rhs; }
1648
1649 template<typename _Tp, typename _Up>
1650 _REQUIRES_NOT_OPTIONAL(_Up)
1651 constexpr auto
1652 operator<= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
1653 -> __optional_le_t<_Tp, _Up>
1654 { return !__lhs || *__lhs <= __rhs; }
1655
1656 template<typename _Tp, typename _Up>
1657 _REQUIRES_NOT_OPTIONAL(_Tp)
1658 constexpr auto
1659 operator<= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
1660 -> __optional_le_t<_Tp, _Up>
1661 { return __rhs && __lhs <= *__rhs; }
1662
1663 template<typename _Tp, typename _Up>
1664 _REQUIRES_NOT_OPTIONAL(_Up)
1665 constexpr auto
1666 operator>= [[nodiscard]] (const optional<_Tp>& __lhs, const _Up& __rhs)
1667 -> __optional_ge_t<_Tp, _Up>
1668 { return __lhs && *__lhs >= __rhs; }
1669
1670 template<typename _Tp, typename _Up>
1671 _REQUIRES_NOT_OPTIONAL(_Tp)
1672 constexpr auto
1673 operator>= [[nodiscard]] (const _Tp& __lhs, const optional<_Up>& __rhs)
1674 -> __optional_ge_t<_Tp, _Up>
1675 { return !__rhs || __lhs >= *__rhs; }
1676
1677#ifdef __cpp_lib_three_way_comparison
1678 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1679 // 3746. optional's spaceship with U with a type derived from optional
1680 // causes infinite constraint meta-recursion
1681 template<typename _Tp>
1682 concept __is_derived_from_optional = requires (const _Tp& __t) {
1683 []<typename _Up>(const optional<_Up>&){ }(__t);
1684 };
1685
1686 template<typename _Tp, typename _Up>
1687 requires (!__is_derived_from_optional<_Up>)
1688 && three_way_comparable_with<_Up, _Tp>
1689 constexpr compare_three_way_result_t<_Tp, _Up>
1690 operator<=> [[nodiscard]] (const optional<_Tp>& __x, const _Up& __v)
1691 { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
1692#endif
1693
1694 // Swap and creation functions.
1695
1696 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1697 // 2748. swappable traits for optionals
1698 template<typename _Tp>
1699 _GLIBCXX20_CONSTEXPR
1700 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
1701 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
1702 noexcept(noexcept(__lhs.swap(__rhs)))
1703 { __lhs.swap(__rhs); }
1704
1705 template<typename _Tp>
1706 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
1707 swap(optional<_Tp>&, optional<_Tp>&) = delete;
1708
1709 template<typename _Tp>
1710 constexpr
1711 enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>,
1712 optional<decay_t<_Tp>>>
1713 make_optional(_Tp&& __t)
1714 noexcept(is_nothrow_constructible_v<optional<decay_t<_Tp>>, _Tp>)
1715 { return optional<decay_t<_Tp>>{ std::forward<_Tp>(__t) }; }
1716
1717 template<typename _Tp, typename... _Args>
1718 constexpr
1719 enable_if_t<is_constructible_v<_Tp, _Args...>,
1720 optional<_Tp>>
1721 make_optional(_Args&&... __args)
1722 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1723 { return optional<_Tp>{ in_place, std::forward<_Args>(__args)... }; }
1724
1725 template<typename _Tp, typename _Up, typename... _Args>
1726 constexpr
1727 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1728 optional<_Tp>>
1729 make_optional(initializer_list<_Up> __il, _Args&&... __args)
1730 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1731 { return optional<_Tp>{ in_place, __il, std::forward<_Args>(__args)... }; }
1732
1733 // Hash.
1734
1735 template<typename _Tp, typename _Up = remove_const_t<_Tp>,
1736 bool = __poison_hash<_Up>::__enable_hash_call>
1737 struct __optional_hash_call_base
1738 {
1739 size_t
1740 operator()(const optional<_Tp>& __t) const
1741 noexcept(noexcept(hash<_Up>{}(*__t)))
1742 {
1743 // We pick an arbitrary hash for disengaged optionals which hopefully
1744 // usual values of _Tp won't typically hash to.
1745 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1746 return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
1747 }
1748 };
1749
1750 template<typename _Tp, typename _Up>
1751 struct __optional_hash_call_base<_Tp, _Up, false> {};
1752
1753 template<typename _Tp>
1754 struct hash<optional<_Tp>>
1755 : private __poison_hash<remove_const_t<_Tp>>,
1756 public __optional_hash_call_base<_Tp>
1757 {
1758 using result_type [[__deprecated__]] = size_t;
1759 using argument_type [[__deprecated__]] = optional<_Tp>;
1760 };
1761
1762 template<typename _Tp>
1763 struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
1764 { };
1765
1766 /// @}
1767
1768#if __cpp_deduction_guides >= 201606
1769 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1770#endif
1771
1772#undef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
1773
1774_GLIBCXX_END_NAMESPACE_VERSION
1775} // namespace std
1776
1777#endif // __cpp_lib_optional
1778
1779#endif // _GLIBCXX_OPTIONAL
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:855
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:869
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:822
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:862
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition complex:400
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:113
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2781
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:127
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:90
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:70
constexpr nullopt_t nullopt
Tag to disengage optional objects.
ISO C++ entities toplevel namespace is std.
constexpr void _Construct(_Tp *__p, _Args &&... __args)
initializer_list