1// <optional> -*- C++ -*-
3// Copyright (C) 2013-2021 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/optional
26 * This is a Standard C++ Library header.
29#ifndef _GLIBCXX_OPTIONAL
30#define _GLIBCXX_OPTIONAL 1
32#pragma GCC system_header
34#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#if __cplusplus > 201703L
49namespace std _GLIBCXX_VISIBILITY(default)
51_GLIBCXX_BEGIN_NAMESPACE_VERSION
54 * @addtogroup utilities
58#if __cplusplus == 201703L
59# define __cpp_lib_optional 201606L
61# define __cpp_lib_optional 202106L
64 template<typename _Tp>
67 /// Tag type to disengage optional objects.
70 // Do not user-declare default constructor at all for
71 // optional_value = {} syntax to work.
72 // nullopt_t() = delete;
74 // Used for constructing nullopt.
75 enum class _Construct { _Token };
77 // Must be constexpr for nullopt_t to be literal.
78 explicit constexpr nullopt_t(_Construct) noexcept { }
81 /// Tag to disengage optional objects.
82 inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
85 * @brief Exception class thrown when a disengaged optional object is
89 class bad_optional_access : public exception
92 bad_optional_access() = default;
93 virtual ~bad_optional_access() = default;
95 const char* what() const noexcept override
96 { return "bad optional access"; }
99 // XXX Does not belong here.
100 [[__noreturn__]] inline void
101 __throw_bad_optional_access()
102 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
104 // This class template manages construction/destruction of
105 // the contained value for a std::optional.
106 template <typename _Tp>
107 struct _Optional_payload_base
109 using _Stored_type = remove_const_t<_Tp>;
111 _Optional_payload_base() = default;
112 ~_Optional_payload_base() = default;
114 template<typename... _Args>
116 _Optional_payload_base(in_place_t __tag, _Args&&... __args)
117 : _M_payload(__tag, std::forward<_Args>(__args)...),
121 template<typename _Up, typename... _Args>
123 _Optional_payload_base(std::initializer_list<_Up> __il,
125 : _M_payload(__il, std::forward<_Args>(__args)...),
129 // Constructor used by _Optional_base copy constructor when the
130 // contained value is not trivially copy constructible.
132 _Optional_payload_base(bool __engaged,
133 const _Optional_payload_base& __other)
135 if (__other._M_engaged)
136 this->_M_construct(__other._M_get());
139 // Constructor used by _Optional_base move constructor when the
140 // contained value is not trivially move constructible.
142 _Optional_payload_base(bool __engaged,
143 _Optional_payload_base&& __other)
145 if (__other._M_engaged)
146 this->_M_construct(std::move(__other._M_get()));
149 // Copy constructor is only used to when the contained value is
150 // trivially copy constructible.
151 _Optional_payload_base(const _Optional_payload_base&) = default;
153 // Move constructor is only used to when the contained value is
154 // trivially copy constructible.
155 _Optional_payload_base(_Optional_payload_base&&) = default;
157 _Optional_payload_base&
158 operator=(const _Optional_payload_base&) = default;
160 _Optional_payload_base&
161 operator=(_Optional_payload_base&&) = default;
163 // used to perform non-trivial copy assignment.
165 _M_copy_assign(const _Optional_payload_base& __other)
167 if (this->_M_engaged && __other._M_engaged)
168 this->_M_get() = __other._M_get();
171 if (__other._M_engaged)
172 this->_M_construct(__other._M_get());
178 // used to perform non-trivial move assignment.
180 _M_move_assign(_Optional_payload_base&& __other)
181 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
182 is_nothrow_move_assignable<_Tp>>)
184 if (this->_M_engaged && __other._M_engaged)
185 this->_M_get() = std::move(__other._M_get());
188 if (__other._M_engaged)
189 this->_M_construct(std::move(__other._M_get()));
195 struct _Empty_byte { };
197 template<typename _Up, bool = is_trivially_destructible_v<_Up>>
200 constexpr _Storage() noexcept : _M_empty() { }
202 template<typename... _Args>
204 _Storage(in_place_t, _Args&&... __args)
205 : _M_value(std::forward<_Args>(__args)...)
208 template<typename _Vp, typename... _Args>
210 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
211 : _M_value(__il, std::forward<_Args>(__args)...)
214 _Empty_byte _M_empty;
218 template<typename _Up>
219 union _Storage<_Up, false>
221 constexpr _Storage() noexcept : _M_empty() { }
223 template<typename... _Args>
225 _Storage(in_place_t, _Args&&... __args)
226 : _M_value(std::forward<_Args>(__args)...)
229 template<typename _Vp, typename... _Args>
231 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
232 : _M_value(__il, std::forward<_Args>(__args)...)
235 // User-provided destructor is needed when _Up has non-trivial dtor.
236 _GLIBCXX20_CONSTEXPR ~_Storage() { }
238 _Empty_byte _M_empty;
242 _Storage<_Stored_type> _M_payload;
244 bool _M_engaged = false;
246 template<typename... _Args>
248 _M_construct(_Args&&... __args)
249 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
251 std::_Construct(std::__addressof(this->_M_payload._M_value),
252 std::forward<_Args>(__args)...);
253 this->_M_engaged = true;
257 _M_destroy() noexcept
260 _M_payload._M_value.~_Stored_type();
263 // The _M_get() operations have _M_engaged as a precondition.
264 // They exist to access the contained value with the appropriate
265 // const-qualification, because _M_payload has had the const removed.
269 { return this->_M_payload._M_value; }
272 _M_get() const noexcept
273 { return this->_M_payload._M_value; }
275 // _M_reset is a 'safe' operation with no precondition.
279 if (this->_M_engaged)
284 // Class template that manages the payload for optionals.
285 template <typename _Tp,
286 bool /*_HasTrivialDestructor*/ =
287 is_trivially_destructible_v<_Tp>,
288 bool /*_HasTrivialCopy */ =
289 is_trivially_copy_assignable_v<_Tp>
290 && is_trivially_copy_constructible_v<_Tp>,
291 bool /*_HasTrivialMove */ =
292 is_trivially_move_assignable_v<_Tp>
293 && is_trivially_move_constructible_v<_Tp>>
294 struct _Optional_payload;
296 // Payload for potentially-constexpr optionals (trivial copy/move/destroy).
297 template <typename _Tp>
298 struct _Optional_payload<_Tp, true, true, true>
299 : _Optional_payload_base<_Tp>
301 using _Optional_payload_base<_Tp>::_Optional_payload_base;
303 _Optional_payload() = default;
306 // Payload for optionals with non-trivial copy construction/assignment.
307 template <typename _Tp>
308 struct _Optional_payload<_Tp, true, false, true>
309 : _Optional_payload_base<_Tp>
311 using _Optional_payload_base<_Tp>::_Optional_payload_base;
313 _Optional_payload() = default;
314 ~_Optional_payload() = default;
315 _Optional_payload(const _Optional_payload&) = default;
316 _Optional_payload(_Optional_payload&&) = default;
317 _Optional_payload& operator=(_Optional_payload&&) = default;
319 // Non-trivial copy assignment.
322 operator=(const _Optional_payload& __other)
324 this->_M_copy_assign(__other);
329 // Payload for optionals with non-trivial move construction/assignment.
330 template <typename _Tp>
331 struct _Optional_payload<_Tp, true, true, false>
332 : _Optional_payload_base<_Tp>
334 using _Optional_payload_base<_Tp>::_Optional_payload_base;
336 _Optional_payload() = default;
337 ~_Optional_payload() = default;
338 _Optional_payload(const _Optional_payload&) = default;
339 _Optional_payload(_Optional_payload&&) = default;
340 _Optional_payload& operator=(const _Optional_payload&) = default;
342 // Non-trivial move assignment.
345 operator=(_Optional_payload&& __other)
346 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
347 is_nothrow_move_assignable<_Tp>>)
349 this->_M_move_assign(std::move(__other));
354 // Payload for optionals with non-trivial copy and move assignment.
355 template <typename _Tp>
356 struct _Optional_payload<_Tp, true, false, false>
357 : _Optional_payload_base<_Tp>
359 using _Optional_payload_base<_Tp>::_Optional_payload_base;
361 _Optional_payload() = default;
362 ~_Optional_payload() = default;
363 _Optional_payload(const _Optional_payload&) = default;
364 _Optional_payload(_Optional_payload&&) = default;
366 // Non-trivial copy assignment.
369 operator=(const _Optional_payload& __other)
371 this->_M_copy_assign(__other);
375 // Non-trivial move assignment.
378 operator=(_Optional_payload&& __other)
379 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
380 is_nothrow_move_assignable<_Tp>>)
382 this->_M_move_assign(std::move(__other));
387 // Payload for optionals with non-trivial destructors.
388 template <typename _Tp, bool _Copy, bool _Move>
389 struct _Optional_payload<_Tp, false, _Copy, _Move>
390 : _Optional_payload<_Tp, true, false, false>
392 // Base class implements all the constructors and assignment operators:
393 using _Optional_payload<_Tp, true, false, false>::_Optional_payload;
394 _Optional_payload() = default;
395 _Optional_payload(const _Optional_payload&) = default;
396 _Optional_payload(_Optional_payload&&) = default;
397 _Optional_payload& operator=(const _Optional_payload&) = default;
398 _Optional_payload& operator=(_Optional_payload&&) = default;
400 // Destructor needs to destroy the contained value:
401 _GLIBCXX20_CONSTEXPR ~_Optional_payload() { this->_M_reset(); }
404 // Common base class for _Optional_base<T> to avoid repeating these
405 // member functions in each specialization.
406 template<typename _Tp, typename _Dp>
407 class _Optional_base_impl
410 using _Stored_type = remove_const_t<_Tp>;
412 // The _M_construct operation has !_M_engaged as a precondition
413 // while _M_destruct has _M_engaged as a precondition.
414 template<typename... _Args>
416 _M_construct(_Args&&... __args)
417 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
419 static_cast<_Dp*>(this)->_M_payload._M_construct(
420 std::forward<_Args>(__args)...);
424 _M_destruct() noexcept
425 { static_cast<_Dp*>(this)->_M_payload._M_destroy(); }
427 // _M_reset is a 'safe' operation with no precondition.
430 { static_cast<_Dp*>(this)->_M_payload._M_reset(); }
432 constexpr bool _M_is_engaged() const noexcept
433 { return static_cast<const _Dp*>(this)->_M_payload._M_engaged; }
435 // The _M_get operations have _M_engaged as a precondition.
439 __glibcxx_assert(this->_M_is_engaged());
440 return static_cast<_Dp*>(this)->_M_payload._M_get();
444 _M_get() const noexcept
446 __glibcxx_assert(this->_M_is_engaged());
447 return static_cast<const _Dp*>(this)->_M_payload._M_get();
452 * @brief Class template that provides copy/move constructors of optional.
454 * Such a separate base class template is necessary in order to
455 * conditionally make copy/move constructors trivial.
457 * When the contained value is trivially copy/move constructible,
458 * the copy/move constructors of _Optional_base will invoke the
459 * trivial copy/move constructor of _Optional_payload. Otherwise,
460 * they will invoke _Optional_payload(bool, const _Optional_payload&)
461 * or _Optional_payload(bool, _Optional_payload&&) to initialize
462 * the contained value, if copying/moving an engaged optional.
464 * Whether the other special members are trivial is determined by the
465 * _Optional_payload<_Tp> specialization used for the _M_payload member.
467 * @see optional, _Enable_special_members
469 template<typename _Tp,
470 bool = is_trivially_copy_constructible_v<_Tp>,
471 bool = is_trivially_move_constructible_v<_Tp>>
472 struct _Optional_base
473 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
475 // Constructors for disengaged optionals.
476 constexpr _Optional_base() = default;
478 // Constructors for engaged optionals.
479 template<typename... _Args,
480 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
482 _Optional_base(in_place_t, _Args&&... __args)
483 : _M_payload(in_place, std::forward<_Args>(__args)...)
486 template<typename _Up, typename... _Args,
487 enable_if_t<is_constructible_v<_Tp,
488 initializer_list<_Up>&,
489 _Args...>, bool> = false>
491 _Optional_base(in_place_t,
492 initializer_list<_Up> __il,
494 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
497 // Copy and move constructors.
499 _Optional_base(const _Optional_base& __other)
500 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
504 _Optional_base(_Optional_base&& __other)
505 noexcept(is_nothrow_move_constructible_v<_Tp>)
506 : _M_payload(__other._M_payload._M_engaged,
507 std::move(__other._M_payload))
510 // Assignment operators.
511 _Optional_base& operator=(const _Optional_base&) = default;
512 _Optional_base& operator=(_Optional_base&&) = default;
514 _Optional_payload<_Tp> _M_payload;
517 template<typename _Tp>
518 struct _Optional_base<_Tp, false, true>
519 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
521 // Constructors for disengaged optionals.
522 constexpr _Optional_base() = default;
524 // Constructors for engaged optionals.
525 template<typename... _Args,
526 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
528 _Optional_base(in_place_t, _Args&&... __args)
529 : _M_payload(in_place, std::forward<_Args>(__args)...)
532 template<typename _Up, typename... _Args,
533 enable_if_t<is_constructible_v<_Tp,
534 initializer_list<_Up>&,
535 _Args...>, bool> = false>
537 _Optional_base(in_place_t,
538 initializer_list<_Up> __il,
540 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
543 // Copy and move constructors.
544 constexpr _Optional_base(const _Optional_base& __other)
545 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
548 constexpr _Optional_base(_Optional_base&& __other) = default;
550 // Assignment operators.
551 _Optional_base& operator=(const _Optional_base&) = default;
552 _Optional_base& operator=(_Optional_base&&) = default;
554 _Optional_payload<_Tp> _M_payload;
557 template<typename _Tp>
558 struct _Optional_base<_Tp, true, false>
559 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
561 // Constructors for disengaged optionals.
562 constexpr _Optional_base() = default;
564 // Constructors for engaged optionals.
565 template<typename... _Args,
566 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
568 _Optional_base(in_place_t, _Args&&... __args)
569 : _M_payload(in_place, std::forward<_Args>(__args)...)
572 template<typename _Up, typename... _Args,
573 enable_if_t<is_constructible_v<_Tp,
574 initializer_list<_Up>&,
575 _Args...>, bool> = false>
577 _Optional_base(in_place_t,
578 initializer_list<_Up> __il,
580 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
583 // Copy and move constructors.
584 constexpr _Optional_base(const _Optional_base& __other) = default;
587 _Optional_base(_Optional_base&& __other)
588 noexcept(is_nothrow_move_constructible_v<_Tp>)
589 : _M_payload(__other._M_payload._M_engaged,
590 std::move(__other._M_payload))
593 // Assignment operators.
594 _Optional_base& operator=(const _Optional_base&) = default;
595 _Optional_base& operator=(_Optional_base&&) = default;
597 _Optional_payload<_Tp> _M_payload;
600 template<typename _Tp>
601 struct _Optional_base<_Tp, true, true>
602 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
604 // Constructors for disengaged optionals.
605 constexpr _Optional_base() = default;
607 // Constructors for engaged optionals.
608 template<typename... _Args,
609 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
611 _Optional_base(in_place_t, _Args&&... __args)
612 : _M_payload(in_place, std::forward<_Args>(__args)...)
615 template<typename _Up, typename... _Args,
616 enable_if_t<is_constructible_v<_Tp,
617 initializer_list<_Up>&,
618 _Args...>, bool> = false>
620 _Optional_base(in_place_t,
621 initializer_list<_Up> __il,
623 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
626 // Copy and move constructors.
627 constexpr _Optional_base(const _Optional_base& __other) = default;
628 constexpr _Optional_base(_Optional_base&& __other) = default;
630 // Assignment operators.
631 _Optional_base& operator=(const _Optional_base&) = default;
632 _Optional_base& operator=(_Optional_base&&) = default;
634 _Optional_payload<_Tp> _M_payload;
637 template<typename _Tp>
640 template<typename _Tp, typename _Up>
641 using __converts_from_optional =
642 __or_<is_constructible<_Tp, const optional<_Up>&>,
643 is_constructible<_Tp, optional<_Up>&>,
644 is_constructible<_Tp, const optional<_Up>&&>,
645 is_constructible<_Tp, optional<_Up>&&>,
646 is_convertible<const optional<_Up>&, _Tp>,
647 is_convertible<optional<_Up>&, _Tp>,
648 is_convertible<const optional<_Up>&&, _Tp>,
649 is_convertible<optional<_Up>&&, _Tp>>;
651 template<typename _Tp, typename _Up>
652 using __assigns_from_optional =
653 __or_<is_assignable<_Tp&, const optional<_Up>&>,
654 is_assignable<_Tp&, optional<_Up>&>,
655 is_assignable<_Tp&, const optional<_Up>&&>,
656 is_assignable<_Tp&, optional<_Up>&&>>;
659 * @brief Class template for optional values.
661 template<typename _Tp>
663 : private _Optional_base<_Tp>,
664 private _Enable_copy_move<
666 is_copy_constructible_v<_Tp>,
668 __and_v<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>,
670 is_move_constructible_v<_Tp>,
672 __and_v<is_move_constructible<_Tp>, is_move_assignable<_Tp>>,
676 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
677 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
678 static_assert(!is_reference_v<_Tp>);
681 using _Base = _Optional_base<_Tp>;
684 template<typename _Up>
685 using __not_self = __not_<is_same<optional, __remove_cvref_t<_Up>>>;
686 template<typename _Up>
687 using __not_tag = __not_<is_same<in_place_t, __remove_cvref_t<_Up>>>;
688 template<typename... _Cond>
689 using _Requires = enable_if_t<__and_v<_Cond...>, bool>;
692 using value_type = _Tp;
694 constexpr optional() noexcept { }
696 constexpr optional(nullopt_t) noexcept { }
698 // Converting constructors for engaged optionals.
699 template<typename _Up = _Tp,
700 _Requires<__not_self<_Up>, __not_tag<_Up>,
701 is_constructible<_Tp, _Up>,
702 is_convertible<_Up, _Tp>> = true>
705 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
706 : _Base(std::in_place, std::forward<_Up>(__t)) { }
708 template<typename _Up = _Tp,
709 _Requires<__not_self<_Up>, __not_tag<_Up>,
710 is_constructible<_Tp, _Up>,
711 __not_<is_convertible<_Up, _Tp>>> = false>
714 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
715 : _Base(std::in_place, std::forward<_Up>(__t)) { }
717 template<typename _Up,
718 _Requires<__not_<is_same<_Tp, _Up>>,
719 is_constructible<_Tp, const _Up&>,
720 is_convertible<const _Up&, _Tp>,
721 __not_<__converts_from_optional<_Tp, _Up>>> = true>
723 optional(const optional<_Up>& __t)
724 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
730 template<typename _Up,
731 _Requires<__not_<is_same<_Tp, _Up>>,
732 is_constructible<_Tp, const _Up&>,
733 __not_<is_convertible<const _Up&, _Tp>>,
734 __not_<__converts_from_optional<_Tp, _Up>>> = false>
736 optional(const optional<_Up>& __t)
737 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
743 template<typename _Up,
744 _Requires<__not_<is_same<_Tp, _Up>>,
745 is_constructible<_Tp, _Up>,
746 is_convertible<_Up, _Tp>,
747 __not_<__converts_from_optional<_Tp, _Up>>> = true>
749 optional(optional<_Up>&& __t)
750 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
753 emplace(std::move(*__t));
756 template<typename _Up,
757 _Requires<__not_<is_same<_Tp, _Up>>,
758 is_constructible<_Tp, _Up>,
759 __not_<is_convertible<_Up, _Tp>>,
760 __not_<__converts_from_optional<_Tp, _Up>>> = false>
762 optional(optional<_Up>&& __t)
763 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
766 emplace(std::move(*__t));
769 template<typename... _Args,
770 _Requires<is_constructible<_Tp, _Args...>> = false>
772 optional(in_place_t, _Args&&... __args)
773 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
774 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
776 template<typename _Up, typename... _Args,
777 _Requires<is_constructible<_Tp,
778 initializer_list<_Up>&,
781 optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
782 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
784 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
787 // Assignment operators.
788 _GLIBCXX20_CONSTEXPR optional&
789 operator=(nullopt_t) noexcept
795 template<typename _Up = _Tp>
797 enable_if_t<__and_v<__not_self<_Up>,
798 __not_<__and_<is_scalar<_Tp>,
799 is_same<_Tp, decay_t<_Up>>>>,
800 is_constructible<_Tp, _Up>,
801 is_assignable<_Tp&, _Up>>,
804 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
805 is_nothrow_assignable<_Tp&, _Up>>)
807 if (this->_M_is_engaged())
808 this->_M_get() = std::forward<_Up>(__u);
810 this->_M_construct(std::forward<_Up>(__u));
815 template<typename _Up>
817 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
818 is_constructible<_Tp, const _Up&>,
819 is_assignable<_Tp&, const _Up&>,
820 __not_<__converts_from_optional<_Tp, _Up>>,
821 __not_<__assigns_from_optional<_Tp, _Up>>>,
823 operator=(const optional<_Up>& __u)
824 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
825 is_nothrow_assignable<_Tp&, const _Up&>>)
829 if (this->_M_is_engaged())
830 this->_M_get() = *__u;
832 this->_M_construct(*__u);
841 template<typename _Up>
843 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
844 is_constructible<_Tp, _Up>,
845 is_assignable<_Tp&, _Up>,
846 __not_<__converts_from_optional<_Tp, _Up>>,
847 __not_<__assigns_from_optional<_Tp, _Up>>>,
849 operator=(optional<_Up>&& __u)
850 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
851 is_nothrow_assignable<_Tp&, _Up>>)
855 if (this->_M_is_engaged())
856 this->_M_get() = std::move(*__u);
858 this->_M_construct(std::move(*__u));
868 template<typename... _Args>
870 enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
871 emplace(_Args&&... __args)
872 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
875 this->_M_construct(std::forward<_Args>(__args)...);
876 return this->_M_get();
879 template<typename _Up, typename... _Args>
881 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
883 emplace(initializer_list<_Up> __il, _Args&&... __args)
884 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
888 this->_M_construct(__il, std::forward<_Args>(__args)...);
889 return this->_M_get();
892 // Destructor is implicit, implemented in _Optional_base.
895 _GLIBCXX20_CONSTEXPR void
896 swap(optional& __other)
897 noexcept(is_nothrow_move_constructible_v<_Tp>
898 && is_nothrow_swappable_v<_Tp>)
902 if (this->_M_is_engaged() && __other._M_is_engaged())
903 swap(this->_M_get(), __other._M_get());
904 else if (this->_M_is_engaged())
906 __other._M_construct(std::move(this->_M_get()));
909 else if (__other._M_is_engaged())
911 this->_M_construct(std::move(__other._M_get()));
912 __other._M_destruct();
918 operator->() const noexcept
919 { return std::__addressof(this->_M_get()); }
922 operator->() noexcept
923 { return std::__addressof(this->_M_get()); }
926 operator*() const& noexcept
927 { return this->_M_get(); }
930 operator*()& noexcept
931 { return this->_M_get(); }
934 operator*()&& noexcept
935 { return std::move(this->_M_get()); }
937 constexpr const _Tp&&
938 operator*() const&& noexcept
939 { return std::move(this->_M_get()); }
941 constexpr explicit operator bool() const noexcept
942 { return this->_M_is_engaged(); }
944 constexpr bool has_value() const noexcept
945 { return this->_M_is_engaged(); }
950 if (this->_M_is_engaged())
951 return this->_M_get();
952 __throw_bad_optional_access();
958 if (this->_M_is_engaged())
959 return this->_M_get();
960 __throw_bad_optional_access();
966 if (this->_M_is_engaged())
967 return std::move(this->_M_get());
968 __throw_bad_optional_access();
971 constexpr const _Tp&&
974 if (this->_M_is_engaged())
975 return std::move(this->_M_get());
976 __throw_bad_optional_access();
979 template<typename _Up>
981 value_or(_Up&& __u) const&
983 static_assert(is_copy_constructible_v<_Tp>);
984 static_assert(is_convertible_v<_Up&&, _Tp>);
986 if (this->_M_is_engaged())
987 return this->_M_get();
989 return static_cast<_Tp>(std::forward<_Up>(__u));
992 template<typename _Up>
994 value_or(_Up&& __u) &&
996 static_assert(is_move_constructible_v<_Tp>);
997 static_assert(is_convertible_v<_Up&&, _Tp>);
999 if (this->_M_is_engaged())
1000 return std::move(this->_M_get());
1002 return static_cast<_Tp>(std::forward<_Up>(__u));
1005 _GLIBCXX20_CONSTEXPR void reset() noexcept { this->_M_reset(); }
1008 template<typename _Tp>
1009 using __optional_relop_t =
1010 enable_if_t<is_convertible<_Tp, bool>::value, bool>;
1012 template<typename _Tp, typename _Up>
1013 using __optional_eq_t = __optional_relop_t<
1014 decltype(std::declval<const _Tp&>() == std::declval<const _Up&>())
1017 template<typename _Tp, typename _Up>
1018 using __optional_ne_t = __optional_relop_t<
1019 decltype(std::declval<const _Tp&>() != std::declval<const _Up&>())
1022 template<typename _Tp, typename _Up>
1023 using __optional_lt_t = __optional_relop_t<
1024 decltype(std::declval<const _Tp&>() < std::declval<const _Up&>())
1027 template<typename _Tp, typename _Up>
1028 using __optional_gt_t = __optional_relop_t<
1029 decltype(std::declval<const _Tp&>() > std::declval<const _Up&>())
1032 template<typename _Tp, typename _Up>
1033 using __optional_le_t = __optional_relop_t<
1034 decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>())
1037 template<typename _Tp, typename _Up>
1038 using __optional_ge_t = __optional_relop_t<
1039 decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>())
1042 // Comparisons between optional values.
1043 template<typename _Tp, typename _Up>
1045 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1046 -> __optional_eq_t<_Tp, _Up>
1048 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
1049 && (!__lhs || *__lhs == *__rhs);
1052 template<typename _Tp, typename _Up>
1054 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1055 -> __optional_ne_t<_Tp, _Up>
1057 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
1058 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
1061 template<typename _Tp, typename _Up>
1063 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1064 -> __optional_lt_t<_Tp, _Up>
1066 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
1069 template<typename _Tp, typename _Up>
1071 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1072 -> __optional_gt_t<_Tp, _Up>
1074 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
1077 template<typename _Tp, typename _Up>
1079 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1080 -> __optional_le_t<_Tp, _Up>
1082 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
1085 template<typename _Tp, typename _Up>
1087 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1088 -> __optional_ge_t<_Tp, _Up>
1090 return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
1093#ifdef __cpp_lib_three_way_comparison
1094 template<typename _Tp, three_way_comparable_with<_Tp> _Up>
1095 constexpr compare_three_way_result_t<_Tp, _Up>
1096 operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
1098 return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
1102 // Comparisons with nullopt.
1103 template<typename _Tp>
1105 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
1108#ifdef __cpp_lib_three_way_comparison
1109 template<typename _Tp>
1110 constexpr strong_ordering
1111 operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
1112 { return bool(__x) <=> false; }
1114 template<typename _Tp>
1116 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
1119 template<typename _Tp>
1121 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1122 { return static_cast<bool>(__lhs); }
1124 template<typename _Tp>
1126 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1127 { return static_cast<bool>(__rhs); }
1129 template<typename _Tp>
1131 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1134 template<typename _Tp>
1136 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
1137 { return static_cast<bool>(__rhs); }
1139 template<typename _Tp>
1141 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
1142 { return static_cast<bool>(__lhs); }
1144 template<typename _Tp>
1146 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1149 template<typename _Tp>
1151 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1154 template<typename _Tp>
1156 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1159 template<typename _Tp>
1161 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1164 template<typename _Tp>
1166 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1168#endif // three-way-comparison
1170 // Comparisons with value type.
1171 template<typename _Tp, typename _Up>
1173 operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
1174 -> __optional_eq_t<_Tp, _Up>
1175 { return __lhs && *__lhs == __rhs; }
1177 template<typename _Tp, typename _Up>
1179 operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
1180 -> __optional_eq_t<_Up, _Tp>
1181 { return __rhs && __lhs == *__rhs; }
1183 template<typename _Tp, typename _Up>
1185 operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
1186 -> __optional_ne_t<_Tp, _Up>
1187 { return !__lhs || *__lhs != __rhs; }
1189 template<typename _Tp, typename _Up>
1191 operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
1192 -> __optional_ne_t<_Up, _Tp>
1193 { return !__rhs || __lhs != *__rhs; }
1195 template<typename _Tp, typename _Up>
1197 operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
1198 -> __optional_lt_t<_Tp, _Up>
1199 { return !__lhs || *__lhs < __rhs; }
1201 template<typename _Tp, typename _Up>
1203 operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
1204 -> __optional_lt_t<_Up, _Tp>
1205 { return __rhs && __lhs < *__rhs; }
1207 template<typename _Tp, typename _Up>
1209 operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
1210 -> __optional_gt_t<_Tp, _Up>
1211 { return __lhs && *__lhs > __rhs; }
1213 template<typename _Tp, typename _Up>
1215 operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
1216 -> __optional_gt_t<_Up, _Tp>
1217 { return !__rhs || __lhs > *__rhs; }
1219 template<typename _Tp, typename _Up>
1221 operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
1222 -> __optional_le_t<_Tp, _Up>
1223 { return !__lhs || *__lhs <= __rhs; }
1225 template<typename _Tp, typename _Up>
1227 operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
1228 -> __optional_le_t<_Up, _Tp>
1229 { return __rhs && __lhs <= *__rhs; }
1231 template<typename _Tp, typename _Up>
1233 operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
1234 -> __optional_ge_t<_Tp, _Up>
1235 { return __lhs && *__lhs >= __rhs; }
1237 template<typename _Tp, typename _Up>
1239 operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
1240 -> __optional_ge_t<_Up, _Tp>
1241 { return !__rhs || __lhs >= *__rhs; }
1243#ifdef __cpp_lib_three_way_comparison
1244 template<typename _Tp>
1245 inline constexpr bool __is_optional_v = false;
1246 template<typename _Tp>
1247 inline constexpr bool __is_optional_v<optional<_Tp>> = true;
1249 template<typename _Tp, typename _Up>
1250 requires (!__is_optional_v<_Up>)
1251 && three_way_comparable_with<_Tp, _Up>
1252 constexpr compare_three_way_result_t<_Tp, _Up>
1253 operator<=>(const optional<_Tp>& __x, const _Up& __v)
1254 { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
1257 // Swap and creation functions.
1259 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1260 // 2748. swappable traits for optionals
1261 template<typename _Tp>
1262 _GLIBCXX20_CONSTEXPR
1263 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
1264 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
1265 noexcept(noexcept(__lhs.swap(__rhs)))
1266 { __lhs.swap(__rhs); }
1268 template<typename _Tp>
1269 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
1270 swap(optional<_Tp>&, optional<_Tp>&) = delete;
1272 template<typename _Tp>
1274 enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>,
1275 optional<decay_t<_Tp>>>
1276 make_optional(_Tp&& __t)
1277 noexcept(is_nothrow_constructible_v<optional<decay_t<_Tp>>, _Tp>)
1278 { return optional<decay_t<_Tp>>{ std::forward<_Tp>(__t) }; }
1280 template<typename _Tp, typename... _Args>
1282 enable_if_t<is_constructible_v<_Tp, _Args...>,
1284 make_optional(_Args&&... __args)
1285 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1286 { return optional<_Tp>{ in_place, std::forward<_Args>(__args)... }; }
1288 template<typename _Tp, typename _Up, typename... _Args>
1290 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1292 make_optional(initializer_list<_Up> __il, _Args&&... __args)
1293 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1294 { return optional<_Tp>{ in_place, __il, std::forward<_Args>(__args)... }; }
1298 template<typename _Tp, typename _Up = remove_const_t<_Tp>,
1299 bool = __poison_hash<_Up>::__enable_hash_call>
1300 struct __optional_hash_call_base
1303 operator()(const optional<_Tp>& __t) const
1304 noexcept(noexcept(hash<_Up>{}(*__t)))
1306 // We pick an arbitrary hash for disengaged optionals which hopefully
1307 // usual values of _Tp won't typically hash to.
1308 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1309 return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
1313 template<typename _Tp, typename _Up>
1314 struct __optional_hash_call_base<_Tp, _Up, false> {};
1316 template<typename _Tp>
1317 struct hash<optional<_Tp>>
1318 : private __poison_hash<remove_const_t<_Tp>>,
1319 public __optional_hash_call_base<_Tp>
1321 using result_type [[__deprecated__]] = size_t;
1322 using argument_type [[__deprecated__]] = optional<_Tp>;
1325 template<typename _Tp>
1326 struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
1331#if __cpp_deduction_guides >= 201606
1332 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1335_GLIBCXX_END_NAMESPACE_VERSION
1340#endif // _GLIBCXX_OPTIONAL