1// <optional> -*- C++ -*-
3// Copyright (C) 2013-2022 Free Software Foundation, Inc.
4// Copyright The GNU Toolchain Authors.
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)
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.
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.
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/>.
26/** @file include/optional
27 * This is a Standard C++ Library header.
30#ifndef _GLIBCXX_OPTIONAL
31#define _GLIBCXX_OPTIONAL 1
33#pragma GCC system_header
35#if __cplusplus >= 201703L
40#include <initializer_list>
41#include <bits/enable_special_members.h>
42#include <bits/exception_defines.h>
43#include <bits/functional_hash.h>
44#include <bits/stl_construct.h> // _Construct
45#include <bits/utility.h> // in_place_t
46#if __cplusplus > 201703L
48# include <bits/invoke.h> // std::__invoke
50#if __cplusplus > 202002L
54namespace std _GLIBCXX_VISIBILITY(default)
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
59 * @addtogroup utilities
63#if __cplusplus > 202002L && __cpp_lib_concepts
64# define __cpp_lib_optional 202110L
65#elif __cplusplus >= 202002L
66# define __cpp_lib_optional 202106L
68# define __cpp_lib_optional 201606L
71 template<typename _Tp>
74 /// Tag type to disengage optional objects.
77 // Do not user-declare default constructor at all for
78 // optional_value = {} syntax to work.
79 // nullopt_t() = delete;
81 // Used for constructing nullopt.
82 enum class _Construct { _Token };
84 // Must be constexpr for nullopt_t to be literal.
85 explicit constexpr nullopt_t(_Construct) noexcept { }
88 /// Tag to disengage optional objects.
89 inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
91 template<typename _Fn> struct _Optional_func { _Fn& _M_f; };
94 * @brief Exception class thrown when a disengaged optional object is
98 class bad_optional_access : public exception
101 bad_optional_access() = default;
102 virtual ~bad_optional_access() = default;
104 const char* what() const noexcept override
105 { return "bad optional access"; }
108 // XXX Does not belong here.
109 [[__noreturn__]] inline void
110 __throw_bad_optional_access()
111 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
113 // This class template manages construction/destruction of
114 // the contained value for a std::optional.
115 template <typename _Tp>
116 struct _Optional_payload_base
118 using _Stored_type = remove_const_t<_Tp>;
120 _Optional_payload_base() = default;
121 ~_Optional_payload_base() = default;
123 template<typename... _Args>
125 _Optional_payload_base(in_place_t __tag, _Args&&... __args)
126 : _M_payload(__tag, std::forward<_Args>(__args)...),
130 template<typename _Up, typename... _Args>
132 _Optional_payload_base(std::initializer_list<_Up> __il,
134 : _M_payload(__il, std::forward<_Args>(__args)...),
138 // Constructor used by _Optional_base copy constructor when the
139 // contained value is not trivially copy constructible.
141 _Optional_payload_base(bool __engaged,
142 const _Optional_payload_base& __other)
144 if (__other._M_engaged)
145 this->_M_construct(__other._M_get());
148 // Constructor used by _Optional_base move constructor when the
149 // contained value is not trivially move constructible.
151 _Optional_payload_base(bool __engaged,
152 _Optional_payload_base&& __other)
154 if (__other._M_engaged)
155 this->_M_construct(std::move(__other._M_get()));
158 // Copy constructor is only used to when the contained value is
159 // trivially copy constructible.
160 _Optional_payload_base(const _Optional_payload_base&) = default;
162 // Move constructor is only used to when the contained value is
163 // trivially copy constructible.
164 _Optional_payload_base(_Optional_payload_base&&) = default;
166 _Optional_payload_base&
167 operator=(const _Optional_payload_base&) = default;
169 _Optional_payload_base&
170 operator=(_Optional_payload_base&&) = default;
172 // used to perform non-trivial copy assignment.
174 _M_copy_assign(const _Optional_payload_base& __other)
176 if (this->_M_engaged && __other._M_engaged)
177 this->_M_get() = __other._M_get();
180 if (__other._M_engaged)
181 this->_M_construct(__other._M_get());
187 // used to perform non-trivial move assignment.
189 _M_move_assign(_Optional_payload_base&& __other)
190 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
191 is_nothrow_move_assignable<_Tp>>)
193 if (this->_M_engaged && __other._M_engaged)
194 this->_M_get() = std::move(__other._M_get());
197 if (__other._M_engaged)
198 this->_M_construct(std::move(__other._M_get()));
204 struct _Empty_byte { };
206 template<typename _Up, bool = is_trivially_destructible_v<_Up>>
209 constexpr _Storage() noexcept : _M_empty() { }
211 template<typename... _Args>
213 _Storage(in_place_t, _Args&&... __args)
214 : _M_value(std::forward<_Args>(__args)...)
217 template<typename _Vp, typename... _Args>
219 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
220 : _M_value(__il, std::forward<_Args>(__args)...)
223#if __cplusplus >= 202002L
224 template<typename _Fn, typename _Arg>
226 _Storage(_Optional_func<_Fn> __f, _Arg&& __arg)
227 : _M_value(std::__invoke(std::forward<_Fn>(__f._M_f),
228 std::forward<_Arg>(__arg)))
232 _Empty_byte _M_empty;
236 template<typename _Up>
237 union _Storage<_Up, false>
239 constexpr _Storage() noexcept : _M_empty() { }
241 template<typename... _Args>
243 _Storage(in_place_t, _Args&&... __args)
244 : _M_value(std::forward<_Args>(__args)...)
247 template<typename _Vp, typename... _Args>
249 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
250 : _M_value(__il, std::forward<_Args>(__args)...)
253#if __cplusplus >= 202002L
254 template<typename _Fn, typename _Arg>
256 _Storage(_Optional_func<_Fn> __f, _Arg&& __arg)
257 : _M_value(std::__invoke(std::forward<_Fn>(__f._M_f),
258 std::forward<_Arg>(__arg)))
262 // User-provided destructor is needed when _Up has non-trivial dtor.
263 _GLIBCXX20_CONSTEXPR ~_Storage() { }
265 _Empty_byte _M_empty;
269 _Storage<_Stored_type> _M_payload;
271 bool _M_engaged = false;
273 template<typename... _Args>
275 _M_construct(_Args&&... __args)
276 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
278 std::_Construct(std::__addressof(this->_M_payload._M_value),
279 std::forward<_Args>(__args)...);
280 this->_M_engaged = true;
284 _M_destroy() noexcept
287 _M_payload._M_value.~_Stored_type();
290#if __cplusplus >= 202002L
291 template<typename _Fn, typename _Up>
293 _M_apply(_Optional_func<_Fn> __f, _Up&& __x)
295 std::construct_at(std::__addressof(this->_M_payload),
296 __f, std::forward<_Up>(__x));
301 // The _M_get() operations have _M_engaged as a precondition.
302 // They exist to access the contained value with the appropriate
303 // const-qualification, because _M_payload has had the const removed.
307 { return this->_M_payload._M_value; }
310 _M_get() const noexcept
311 { return this->_M_payload._M_value; }
313 // _M_reset is a 'safe' operation with no precondition.
317 if (this->_M_engaged)
322 // Class template that manages the payload for optionals.
323 template <typename _Tp,
324 bool /*_HasTrivialDestructor*/ =
325 is_trivially_destructible_v<_Tp>,
326 bool /*_HasTrivialCopy */ =
327 is_trivially_copy_assignable_v<_Tp>
328 && is_trivially_copy_constructible_v<_Tp>,
329 bool /*_HasTrivialMove */ =
330 is_trivially_move_assignable_v<_Tp>
331 && is_trivially_move_constructible_v<_Tp>>
332 struct _Optional_payload;
334 // Payload for potentially-constexpr optionals (trivial copy/move/destroy).
335 template <typename _Tp>
336 struct _Optional_payload<_Tp, true, true, true>
337 : _Optional_payload_base<_Tp>
339 using _Optional_payload_base<_Tp>::_Optional_payload_base;
341 _Optional_payload() = default;
344 // Payload for optionals with non-trivial copy construction/assignment.
345 template <typename _Tp>
346 struct _Optional_payload<_Tp, true, false, true>
347 : _Optional_payload_base<_Tp>
349 using _Optional_payload_base<_Tp>::_Optional_payload_base;
351 _Optional_payload() = default;
352 ~_Optional_payload() = default;
353 _Optional_payload(const _Optional_payload&) = default;
354 _Optional_payload(_Optional_payload&&) = default;
355 _Optional_payload& operator=(_Optional_payload&&) = default;
357 // Non-trivial copy assignment.
360 operator=(const _Optional_payload& __other)
362 this->_M_copy_assign(__other);
367 // Payload for optionals with non-trivial move construction/assignment.
368 template <typename _Tp>
369 struct _Optional_payload<_Tp, true, true, false>
370 : _Optional_payload_base<_Tp>
372 using _Optional_payload_base<_Tp>::_Optional_payload_base;
374 _Optional_payload() = default;
375 ~_Optional_payload() = default;
376 _Optional_payload(const _Optional_payload&) = default;
377 _Optional_payload(_Optional_payload&&) = default;
378 _Optional_payload& operator=(const _Optional_payload&) = default;
380 // Non-trivial move assignment.
383 operator=(_Optional_payload&& __other)
384 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
385 is_nothrow_move_assignable<_Tp>>)
387 this->_M_move_assign(std::move(__other));
392 // Payload for optionals with non-trivial copy and move assignment.
393 template <typename _Tp>
394 struct _Optional_payload<_Tp, true, false, false>
395 : _Optional_payload_base<_Tp>
397 using _Optional_payload_base<_Tp>::_Optional_payload_base;
399 _Optional_payload() = default;
400 ~_Optional_payload() = default;
401 _Optional_payload(const _Optional_payload&) = default;
402 _Optional_payload(_Optional_payload&&) = default;
404 // Non-trivial copy assignment.
407 operator=(const _Optional_payload& __other)
409 this->_M_copy_assign(__other);
413 // Non-trivial move assignment.
416 operator=(_Optional_payload&& __other)
417 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
418 is_nothrow_move_assignable<_Tp>>)
420 this->_M_move_assign(std::move(__other));
425 // Payload for optionals with non-trivial destructors.
426 template <typename _Tp, bool _Copy, bool _Move>
427 struct _Optional_payload<_Tp, false, _Copy, _Move>
428 : _Optional_payload<_Tp, true, false, false>
430 // Base class implements all the constructors and assignment operators:
431 using _Optional_payload<_Tp, true, false, false>::_Optional_payload;
432 _Optional_payload() = default;
433 _Optional_payload(const _Optional_payload&) = default;
434 _Optional_payload(_Optional_payload&&) = default;
435 _Optional_payload& operator=(const _Optional_payload&) = default;
436 _Optional_payload& operator=(_Optional_payload&&) = default;
438 // Destructor needs to destroy the contained value:
439 _GLIBCXX20_CONSTEXPR ~_Optional_payload() { this->_M_reset(); }
442 // Common base class for _Optional_base<T> to avoid repeating these
443 // member functions in each specialization.
444 template<typename _Tp, typename _Dp>
445 class _Optional_base_impl
448 using _Stored_type = remove_const_t<_Tp>;
450 // The _M_construct operation has !_M_engaged as a precondition
451 // while _M_destruct has _M_engaged as a precondition.
452 template<typename... _Args>
454 _M_construct(_Args&&... __args)
455 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
457 static_cast<_Dp*>(this)->_M_payload._M_construct(
458 std::forward<_Args>(__args)...);
462 _M_destruct() noexcept
463 { static_cast<_Dp*>(this)->_M_payload._M_destroy(); }
465 // _M_reset is a 'safe' operation with no precondition.
468 { static_cast<_Dp*>(this)->_M_payload._M_reset(); }
470 constexpr bool _M_is_engaged() const noexcept
471 { return static_cast<const _Dp*>(this)->_M_payload._M_engaged; }
473 // The _M_get operations have _M_engaged as a precondition.
477 __glibcxx_assert(this->_M_is_engaged());
478 return static_cast<_Dp*>(this)->_M_payload._M_get();
482 _M_get() const noexcept
484 __glibcxx_assert(this->_M_is_engaged());
485 return static_cast<const _Dp*>(this)->_M_payload._M_get();
490 * @brief Class template that provides copy/move constructors of optional.
492 * Such a separate base class template is necessary in order to
493 * conditionally make copy/move constructors trivial.
495 * When the contained value is trivially copy/move constructible,
496 * the copy/move constructors of _Optional_base will invoke the
497 * trivial copy/move constructor of _Optional_payload. Otherwise,
498 * they will invoke _Optional_payload(bool, const _Optional_payload&)
499 * or _Optional_payload(bool, _Optional_payload&&) to initialize
500 * the contained value, if copying/moving an engaged optional.
502 * Whether the other special members are trivial is determined by the
503 * _Optional_payload<_Tp> specialization used for the _M_payload member.
505 * @see optional, _Enable_special_members
507 template<typename _Tp,
508 bool = is_trivially_copy_constructible_v<_Tp>,
509 bool = is_trivially_move_constructible_v<_Tp>>
510 struct _Optional_base
511 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
513 // Constructors for disengaged optionals.
514 constexpr _Optional_base() = default;
516 // Constructors for engaged optionals.
517 template<typename... _Args,
518 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
520 _Optional_base(in_place_t, _Args&&... __args)
521 : _M_payload(in_place, std::forward<_Args>(__args)...)
524 template<typename _Up, typename... _Args,
525 enable_if_t<is_constructible_v<_Tp,
526 initializer_list<_Up>&,
527 _Args...>, bool> = false>
529 _Optional_base(in_place_t,
530 initializer_list<_Up> __il,
532 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
535 // Copy and move constructors.
537 _Optional_base(const _Optional_base& __other)
538 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
542 _Optional_base(_Optional_base&& __other)
543 noexcept(is_nothrow_move_constructible_v<_Tp>)
544 : _M_payload(__other._M_payload._M_engaged,
545 std::move(__other._M_payload))
548 // Assignment operators.
549 _Optional_base& operator=(const _Optional_base&) = default;
550 _Optional_base& operator=(_Optional_base&&) = default;
552 _Optional_payload<_Tp> _M_payload;
555 template<typename _Tp>
556 struct _Optional_base<_Tp, false, true>
557 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
559 // Constructors for disengaged optionals.
560 constexpr _Optional_base() = default;
562 // Constructors for engaged optionals.
563 template<typename... _Args,
564 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
566 _Optional_base(in_place_t, _Args&&... __args)
567 : _M_payload(in_place, std::forward<_Args>(__args)...)
570 template<typename _Up, typename... _Args,
571 enable_if_t<is_constructible_v<_Tp,
572 initializer_list<_Up>&,
573 _Args...>, bool> = false>
575 _Optional_base(in_place_t,
576 initializer_list<_Up> __il,
578 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
581 // Copy and move constructors.
582 constexpr _Optional_base(const _Optional_base& __other)
583 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
586 constexpr _Optional_base(_Optional_base&& __other) = default;
588 // Assignment operators.
589 _Optional_base& operator=(const _Optional_base&) = default;
590 _Optional_base& operator=(_Optional_base&&) = default;
592 _Optional_payload<_Tp> _M_payload;
595 template<typename _Tp>
596 struct _Optional_base<_Tp, true, false>
597 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
599 // Constructors for disengaged optionals.
600 constexpr _Optional_base() = default;
602 // Constructors for engaged optionals.
603 template<typename... _Args,
604 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
606 _Optional_base(in_place_t, _Args&&... __args)
607 : _M_payload(in_place, std::forward<_Args>(__args)...)
610 template<typename _Up, typename... _Args,
611 enable_if_t<is_constructible_v<_Tp,
612 initializer_list<_Up>&,
613 _Args...>, bool> = false>
615 _Optional_base(in_place_t,
616 initializer_list<_Up> __il,
618 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
621 // Copy and move constructors.
622 constexpr _Optional_base(const _Optional_base& __other) = default;
625 _Optional_base(_Optional_base&& __other)
626 noexcept(is_nothrow_move_constructible_v<_Tp>)
627 : _M_payload(__other._M_payload._M_engaged,
628 std::move(__other._M_payload))
631 // Assignment operators.
632 _Optional_base& operator=(const _Optional_base&) = default;
633 _Optional_base& operator=(_Optional_base&&) = default;
635 _Optional_payload<_Tp> _M_payload;
638 template<typename _Tp>
639 struct _Optional_base<_Tp, true, true>
640 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
642 // Constructors for disengaged optionals.
643 constexpr _Optional_base() = default;
645 // Constructors for engaged optionals.
646 template<typename... _Args,
647 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
649 _Optional_base(in_place_t, _Args&&... __args)
650 : _M_payload(in_place, std::forward<_Args>(__args)...)
653 template<typename _Up, typename... _Args,
654 enable_if_t<is_constructible_v<_Tp,
655 initializer_list<_Up>&,
656 _Args...>, bool> = false>
658 _Optional_base(in_place_t,
659 initializer_list<_Up> __il,
661 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
664 // Copy and move constructors.
665 constexpr _Optional_base(const _Optional_base& __other) = default;
666 constexpr _Optional_base(_Optional_base&& __other) = default;
668 // Assignment operators.
669 _Optional_base& operator=(const _Optional_base&) = default;
670 _Optional_base& operator=(_Optional_base&&) = default;
672 _Optional_payload<_Tp> _M_payload;
675 template<typename _Tp>
678 template<typename _Tp>
679 inline constexpr bool __is_optional_v = false;
680 template<typename _Tp>
681 inline constexpr bool __is_optional_v<optional<_Tp>> = true;
683 template<typename _Tp, typename _Up>
684 using __converts_from_optional =
685 __or_<is_constructible<_Tp, const optional<_Up>&>,
686 is_constructible<_Tp, optional<_Up>&>,
687 is_constructible<_Tp, const optional<_Up>&&>,
688 is_constructible<_Tp, optional<_Up>&&>,
689 is_convertible<const optional<_Up>&, _Tp>,
690 is_convertible<optional<_Up>&, _Tp>,
691 is_convertible<const optional<_Up>&&, _Tp>,
692 is_convertible<optional<_Up>&&, _Tp>>;
694 template<typename _Tp, typename _Up>
695 using __assigns_from_optional =
696 __or_<is_assignable<_Tp&, const optional<_Up>&>,
697 is_assignable<_Tp&, optional<_Up>&>,
698 is_assignable<_Tp&, const optional<_Up>&&>,
699 is_assignable<_Tp&, optional<_Up>&&>>;
702 * @brief Class template for optional values.
704 template<typename _Tp>
706 : private _Optional_base<_Tp>,
707 private _Enable_copy_move<
709 is_copy_constructible_v<_Tp>,
711 __and_v<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>,
713 is_move_constructible_v<_Tp>,
715 __and_v<is_move_constructible<_Tp>, is_move_assignable<_Tp>>,
719 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
720 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
721 static_assert(!is_reference_v<_Tp>);
724 using _Base = _Optional_base<_Tp>;
727 template<typename _Up>
728 using __not_self = __not_<is_same<optional, __remove_cvref_t<_Up>>>;
729 template<typename _Up>
730 using __not_tag = __not_<is_same<in_place_t, __remove_cvref_t<_Up>>>;
731 template<typename... _Cond>
732 using _Requires = enable_if_t<__and_v<_Cond...>, bool>;
735 using value_type = _Tp;
737 constexpr optional() noexcept { }
739 constexpr optional(nullopt_t) noexcept { }
741 // Converting constructors for engaged optionals.
742 template<typename _Up = _Tp,
743 _Requires<__not_self<_Up>, __not_tag<_Up>,
744 is_constructible<_Tp, _Up>,
745 is_convertible<_Up, _Tp>> = true>
748 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
749 : _Base(std::in_place, std::forward<_Up>(__t)) { }
751 template<typename _Up = _Tp,
752 _Requires<__not_self<_Up>, __not_tag<_Up>,
753 is_constructible<_Tp, _Up>,
754 __not_<is_convertible<_Up, _Tp>>> = false>
757 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
758 : _Base(std::in_place, std::forward<_Up>(__t)) { }
760 template<typename _Up,
761 _Requires<__not_<is_same<_Tp, _Up>>,
762 is_constructible<_Tp, const _Up&>,
763 is_convertible<const _Up&, _Tp>,
764 __not_<__converts_from_optional<_Tp, _Up>>> = true>
766 optional(const optional<_Up>& __t)
767 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
773 template<typename _Up,
774 _Requires<__not_<is_same<_Tp, _Up>>,
775 is_constructible<_Tp, const _Up&>,
776 __not_<is_convertible<const _Up&, _Tp>>,
777 __not_<__converts_from_optional<_Tp, _Up>>> = false>
779 optional(const optional<_Up>& __t)
780 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
786 template<typename _Up,
787 _Requires<__not_<is_same<_Tp, _Up>>,
788 is_constructible<_Tp, _Up>,
789 is_convertible<_Up, _Tp>,
790 __not_<__converts_from_optional<_Tp, _Up>>> = true>
792 optional(optional<_Up>&& __t)
793 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
796 emplace(std::move(*__t));
799 template<typename _Up,
800 _Requires<__not_<is_same<_Tp, _Up>>,
801 is_constructible<_Tp, _Up>,
802 __not_<is_convertible<_Up, _Tp>>,
803 __not_<__converts_from_optional<_Tp, _Up>>> = false>
805 optional(optional<_Up>&& __t)
806 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
809 emplace(std::move(*__t));
812 template<typename... _Args,
813 _Requires<is_constructible<_Tp, _Args...>> = false>
815 optional(in_place_t, _Args&&... __args)
816 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
817 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
819 template<typename _Up, typename... _Args,
820 _Requires<is_constructible<_Tp,
821 initializer_list<_Up>&,
824 optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
825 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
827 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
830 // Assignment operators.
831 _GLIBCXX20_CONSTEXPR optional&
832 operator=(nullopt_t) noexcept
838 template<typename _Up = _Tp>
840 enable_if_t<__and_v<__not_self<_Up>,
841 __not_<__and_<is_scalar<_Tp>,
842 is_same<_Tp, decay_t<_Up>>>>,
843 is_constructible<_Tp, _Up>,
844 is_assignable<_Tp&, _Up>>,
847 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
848 is_nothrow_assignable<_Tp&, _Up>>)
850 if (this->_M_is_engaged())
851 this->_M_get() = std::forward<_Up>(__u);
853 this->_M_construct(std::forward<_Up>(__u));
858 template<typename _Up>
860 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
861 is_constructible<_Tp, const _Up&>,
862 is_assignable<_Tp&, const _Up&>,
863 __not_<__converts_from_optional<_Tp, _Up>>,
864 __not_<__assigns_from_optional<_Tp, _Up>>>,
866 operator=(const optional<_Up>& __u)
867 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
868 is_nothrow_assignable<_Tp&, const _Up&>>)
872 if (this->_M_is_engaged())
873 this->_M_get() = *__u;
875 this->_M_construct(*__u);
884 template<typename _Up>
886 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
887 is_constructible<_Tp, _Up>,
888 is_assignable<_Tp&, _Up>,
889 __not_<__converts_from_optional<_Tp, _Up>>,
890 __not_<__assigns_from_optional<_Tp, _Up>>>,
892 operator=(optional<_Up>&& __u)
893 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
894 is_nothrow_assignable<_Tp&, _Up>>)
898 if (this->_M_is_engaged())
899 this->_M_get() = std::move(*__u);
901 this->_M_construct(std::move(*__u));
911 template<typename... _Args>
913 enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
914 emplace(_Args&&... __args)
915 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
918 this->_M_construct(std::forward<_Args>(__args)...);
919 return this->_M_get();
922 template<typename _Up, typename... _Args>
924 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
926 emplace(initializer_list<_Up> __il, _Args&&... __args)
927 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
931 this->_M_construct(__il, std::forward<_Args>(__args)...);
932 return this->_M_get();
935 // Destructor is implicit, implemented in _Optional_base.
938 _GLIBCXX20_CONSTEXPR void
939 swap(optional& __other)
940 noexcept(is_nothrow_move_constructible_v<_Tp>
941 && is_nothrow_swappable_v<_Tp>)
945 if (this->_M_is_engaged() && __other._M_is_engaged())
946 swap(this->_M_get(), __other._M_get());
947 else if (this->_M_is_engaged())
949 __other._M_construct(std::move(this->_M_get()));
952 else if (__other._M_is_engaged())
954 this->_M_construct(std::move(__other._M_get()));
955 __other._M_destruct();
961 operator->() const noexcept
962 { return std::__addressof(this->_M_get()); }
965 operator->() noexcept
966 { return std::__addressof(this->_M_get()); }
969 operator*() const& noexcept
970 { return this->_M_get(); }
973 operator*()& noexcept
974 { return this->_M_get(); }
977 operator*()&& noexcept
978 { return std::move(this->_M_get()); }
980 constexpr const _Tp&&
981 operator*() const&& noexcept
982 { return std::move(this->_M_get()); }
984 constexpr explicit operator bool() const noexcept
985 { return this->_M_is_engaged(); }
987 constexpr bool has_value() const noexcept
988 { return this->_M_is_engaged(); }
993 if (this->_M_is_engaged())
994 return this->_M_get();
995 __throw_bad_optional_access();
1001 if (this->_M_is_engaged())
1002 return this->_M_get();
1003 __throw_bad_optional_access();
1009 if (this->_M_is_engaged())
1010 return std::move(this->_M_get());
1011 __throw_bad_optional_access();
1014 constexpr const _Tp&&
1017 if (this->_M_is_engaged())
1018 return std::move(this->_M_get());
1019 __throw_bad_optional_access();
1022 template<typename _Up>
1024 value_or(_Up&& __u) const&
1026 static_assert(is_copy_constructible_v<_Tp>);
1027 static_assert(is_convertible_v<_Up&&, _Tp>);
1029 if (this->_M_is_engaged())
1030 return this->_M_get();
1032 return static_cast<_Tp>(std::forward<_Up>(__u));
1035 template<typename _Up>
1037 value_or(_Up&& __u) &&
1039 static_assert(is_move_constructible_v<_Tp>);
1040 static_assert(is_convertible_v<_Up&&, _Tp>);
1042 if (this->_M_is_engaged())
1043 return std::move(this->_M_get());
1045 return static_cast<_Tp>(std::forward<_Up>(__u));
1048#if __cpp_lib_optional >= 202110L
1049 // [optional.monadic]
1051 template<typename _Fn>
1053 and_then(_Fn&& __f) &
1055 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp&>>;
1056 static_assert(__is_optional_v<remove_cvref_t<_Up>>);
1058 return std::__invoke(std::forward<_Fn>(__f), **this);
1063 template<typename _Fn>
1065 and_then(_Fn&& __f) const &
1067 using _Up = remove_cvref_t<invoke_result_t<_Fn, const _Tp&>>;
1068 static_assert(__is_optional_v<_Up>);
1070 return std::__invoke(std::forward<_Fn>(__f), **this);
1075 template<typename _Fn>
1077 and_then(_Fn&& __f) &&
1079 using _Up = remove_cvref_t<invoke_result_t<_Fn, _Tp>>;
1080 static_assert(__is_optional_v<remove_cvref_t<_Up>>);
1082 return std::__invoke(std::forward<_Fn>(__f), std::move(**this));
1087 template<typename _Fn>
1089 and_then(_Fn&& __f) const &&
1091 using _Up = remove_cvref_t<invoke_result_t<_Fn, const _Tp>>;
1092 static_assert(__is_optional_v<remove_cvref_t<_Up>>);
1094 return std::__invoke(std::forward<_Fn>(__f), std::move(**this));
1099 template<typename _Fn>
1101 transform(_Fn&& __f) &
1103 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp&>>;
1105 return optional<_Up>(_Optional_func<_Fn>{__f}, **this);
1107 return optional<_Up>();
1110 template<typename _Fn>
1112 transform(_Fn&& __f) const &
1114 using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp&>>;
1116 return optional<_Up>(_Optional_func<_Fn>{__f}, **this);
1118 return optional<_Up>();
1121 template<typename _Fn>
1123 transform(_Fn&& __f) &&
1125 using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp>>;
1127 return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this));
1129 return optional<_Up>();
1132 template<typename _Fn>
1134 transform(_Fn&& __f) const &&
1136 using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp>>;
1138 return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this));
1140 return optional<_Up>();
1143 template<typename _Fn> requires invocable<_Fn> && copy_constructible<_Tp>
1145 or_else(_Fn&& __f) const&
1147 using _Up = invoke_result_t<_Fn>;
1148 static_assert( is_same_v<remove_cvref_t<_Up>, optional> );
1153 return std::forward<_Fn>(__f)();
1156 template<typename _Fn> requires invocable<_Fn> && move_constructible<_Tp>
1158 or_else(_Fn&& __f) &&
1160 using _Up = invoke_result_t<_Fn>;
1161 static_assert( is_same_v<remove_cvref_t<_Up>, optional> );
1164 return std::move(*this);
1166 return std::forward<_Fn>(__f)();
1170 _GLIBCXX20_CONSTEXPR void reset() noexcept { this->_M_reset(); }
1173#if __cplusplus >= 202002L
1174 template<typename _Up> friend class optional;
1176 template<typename _Fn, typename _Value>
1178 optional(_Optional_func<_Fn> __f, _Value&& __v)
1180 this->_M_payload._M_apply(__f, std::forward<_Value>(__v));
1185 template<typename _Tp>
1186 using __optional_relop_t =
1187 enable_if_t<is_convertible<_Tp, bool>::value, bool>;
1189 template<typename _Tp, typename _Up>
1190 using __optional_eq_t = __optional_relop_t<
1191 decltype(std::declval<const _Tp&>() == std::declval<const _Up&>())
1194 template<typename _Tp, typename _Up>
1195 using __optional_ne_t = __optional_relop_t<
1196 decltype(std::declval<const _Tp&>() != std::declval<const _Up&>())
1199 template<typename _Tp, typename _Up>
1200 using __optional_lt_t = __optional_relop_t<
1201 decltype(std::declval<const _Tp&>() < std::declval<const _Up&>())
1204 template<typename _Tp, typename _Up>
1205 using __optional_gt_t = __optional_relop_t<
1206 decltype(std::declval<const _Tp&>() > std::declval<const _Up&>())
1209 template<typename _Tp, typename _Up>
1210 using __optional_le_t = __optional_relop_t<
1211 decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>())
1214 template<typename _Tp, typename _Up>
1215 using __optional_ge_t = __optional_relop_t<
1216 decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>())
1219 // Comparisons between optional values.
1220 template<typename _Tp, typename _Up>
1222 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1223 -> __optional_eq_t<_Tp, _Up>
1225 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
1226 && (!__lhs || *__lhs == *__rhs);
1229 template<typename _Tp, typename _Up>
1231 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1232 -> __optional_ne_t<_Tp, _Up>
1234 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
1235 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
1238 template<typename _Tp, typename _Up>
1240 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1241 -> __optional_lt_t<_Tp, _Up>
1243 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
1246 template<typename _Tp, typename _Up>
1248 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1249 -> __optional_gt_t<_Tp, _Up>
1251 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
1254 template<typename _Tp, typename _Up>
1256 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1257 -> __optional_le_t<_Tp, _Up>
1259 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
1262 template<typename _Tp, typename _Up>
1264 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1265 -> __optional_ge_t<_Tp, _Up>
1267 return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
1270#ifdef __cpp_lib_three_way_comparison
1271 template<typename _Tp, three_way_comparable_with<_Tp> _Up>
1272 constexpr compare_three_way_result_t<_Tp, _Up>
1273 operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
1275 return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
1279 // Comparisons with nullopt.
1280 template<typename _Tp>
1282 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
1285#ifdef __cpp_lib_three_way_comparison
1286 template<typename _Tp>
1287 constexpr strong_ordering
1288 operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
1289 { return bool(__x) <=> false; }
1291 template<typename _Tp>
1293 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
1296 template<typename _Tp>
1298 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1299 { return static_cast<bool>(__lhs); }
1301 template<typename _Tp>
1303 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1304 { return static_cast<bool>(__rhs); }
1306 template<typename _Tp>
1308 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1311 template<typename _Tp>
1313 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
1314 { return static_cast<bool>(__rhs); }
1316 template<typename _Tp>
1318 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
1319 { return static_cast<bool>(__lhs); }
1321 template<typename _Tp>
1323 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1326 template<typename _Tp>
1328 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1331 template<typename _Tp>
1333 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1336 template<typename _Tp>
1338 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1341 template<typename _Tp>
1343 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1345#endif // three-way-comparison
1347 // Comparisons with value type.
1348 template<typename _Tp, typename _Up>
1350 operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
1351 -> __optional_eq_t<_Tp, _Up>
1352 { return __lhs && *__lhs == __rhs; }
1354 template<typename _Tp, typename _Up>
1356 operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
1357 -> __optional_eq_t<_Up, _Tp>
1358 { return __rhs && __lhs == *__rhs; }
1360 template<typename _Tp, typename _Up>
1362 operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
1363 -> __optional_ne_t<_Tp, _Up>
1364 { return !__lhs || *__lhs != __rhs; }
1366 template<typename _Tp, typename _Up>
1368 operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
1369 -> __optional_ne_t<_Up, _Tp>
1370 { return !__rhs || __lhs != *__rhs; }
1372 template<typename _Tp, typename _Up>
1374 operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
1375 -> __optional_lt_t<_Tp, _Up>
1376 { return !__lhs || *__lhs < __rhs; }
1378 template<typename _Tp, typename _Up>
1380 operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
1381 -> __optional_lt_t<_Up, _Tp>
1382 { return __rhs && __lhs < *__rhs; }
1384 template<typename _Tp, typename _Up>
1386 operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
1387 -> __optional_gt_t<_Tp, _Up>
1388 { return __lhs && *__lhs > __rhs; }
1390 template<typename _Tp, typename _Up>
1392 operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
1393 -> __optional_gt_t<_Up, _Tp>
1394 { return !__rhs || __lhs > *__rhs; }
1396 template<typename _Tp, typename _Up>
1398 operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
1399 -> __optional_le_t<_Tp, _Up>
1400 { return !__lhs || *__lhs <= __rhs; }
1402 template<typename _Tp, typename _Up>
1404 operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
1405 -> __optional_le_t<_Up, _Tp>
1406 { return __rhs && __lhs <= *__rhs; }
1408 template<typename _Tp, typename _Up>
1410 operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
1411 -> __optional_ge_t<_Tp, _Up>
1412 { return __lhs && *__lhs >= __rhs; }
1414 template<typename _Tp, typename _Up>
1416 operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
1417 -> __optional_ge_t<_Up, _Tp>
1418 { return !__rhs || __lhs >= *__rhs; }
1420#ifdef __cpp_lib_three_way_comparison
1421 template<typename _Tp, typename _Up>
1422 requires (!__is_optional_v<_Up>)
1423 && three_way_comparable_with<_Tp, _Up>
1424 constexpr compare_three_way_result_t<_Tp, _Up>
1425 operator<=>(const optional<_Tp>& __x, const _Up& __v)
1426 { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
1429 // Swap and creation functions.
1431 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1432 // 2748. swappable traits for optionals
1433 template<typename _Tp>
1434 _GLIBCXX20_CONSTEXPR
1435 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
1436 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
1437 noexcept(noexcept(__lhs.swap(__rhs)))
1438 { __lhs.swap(__rhs); }
1440 template<typename _Tp>
1441 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
1442 swap(optional<_Tp>&, optional<_Tp>&) = delete;
1444 template<typename _Tp>
1446 enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>,
1447 optional<decay_t<_Tp>>>
1448 make_optional(_Tp&& __t)
1449 noexcept(is_nothrow_constructible_v<optional<decay_t<_Tp>>, _Tp>)
1450 { return optional<decay_t<_Tp>>{ std::forward<_Tp>(__t) }; }
1452 template<typename _Tp, typename... _Args>
1454 enable_if_t<is_constructible_v<_Tp, _Args...>,
1456 make_optional(_Args&&... __args)
1457 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1458 { return optional<_Tp>{ in_place, std::forward<_Args>(__args)... }; }
1460 template<typename _Tp, typename _Up, typename... _Args>
1462 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1464 make_optional(initializer_list<_Up> __il, _Args&&... __args)
1465 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1466 { return optional<_Tp>{ in_place, __il, std::forward<_Args>(__args)... }; }
1470 template<typename _Tp, typename _Up = remove_const_t<_Tp>,
1471 bool = __poison_hash<_Up>::__enable_hash_call>
1472 struct __optional_hash_call_base
1475 operator()(const optional<_Tp>& __t) const
1476 noexcept(noexcept(hash<_Up>{}(*__t)))
1478 // We pick an arbitrary hash for disengaged optionals which hopefully
1479 // usual values of _Tp won't typically hash to.
1480 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1481 return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
1485 template<typename _Tp, typename _Up>
1486 struct __optional_hash_call_base<_Tp, _Up, false> {};
1488 template<typename _Tp>
1489 struct hash<optional<_Tp>>
1490 : private __poison_hash<remove_const_t<_Tp>>,
1491 public __optional_hash_call_base<_Tp>
1493 using result_type [[__deprecated__]] = size_t;
1494 using argument_type [[__deprecated__]] = optional<_Tp>;
1497 template<typename _Tp>
1498 struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
1503#if __cpp_deduction_guides >= 201606
1504 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1507_GLIBCXX_END_NAMESPACE_VERSION
1512#endif // _GLIBCXX_OPTIONAL