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