1 // <optional> -*- C++ -*-
3 // Copyright (C) 2013-2020 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
37 #include <type_traits>
40 #include <initializer_list>
41 #include <bits/exception_defines.h>
42 #include <bits/functional_hash.h>
43 #include <bits/enable_special_members.h>
44 #if __cplusplus > 201703L
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 * @addtogroup utilities
57 #define __cpp_lib_optional 201606L
59 template<typename _Tp>
62 /// Tag type to disengage optional objects.
65 // Do not user-declare default constructor at all for
66 // optional_value = {} syntax to work.
67 // nullopt_t() = delete;
69 // Used for constructing nullopt.
70 enum class _Construct { _Token };
72 // Must be constexpr for nullopt_t to be literal.
73 explicit constexpr nullopt_t(_Construct) { }
76 /// Tag to disengage optional objects.
77 inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
80 * @brief Exception class thrown when a disengaged optional object is
84 class bad_optional_access : public exception
87 bad_optional_access() = default;
88 virtual ~bad_optional_access() = default;
90 const char* what() const noexcept override
91 { return "bad optional access"; }
95 __throw_bad_optional_access()
96 __attribute__((__noreturn__));
98 // XXX Does not belong here.
100 __throw_bad_optional_access()
101 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
103 // This class template manages construction/destruction of
104 // the contained value for a std::optional.
105 template <typename _Tp>
106 struct _Optional_payload_base
108 using _Stored_type = remove_const_t<_Tp>;
110 _Optional_payload_base() = default;
111 ~_Optional_payload_base() = default;
113 template<typename... _Args>
115 _Optional_payload_base(in_place_t __tag, _Args&&... __args)
116 : _M_payload(__tag, std::forward<_Args>(__args)...),
120 template<typename _Up, typename... _Args>
122 _Optional_payload_base(std::initializer_list<_Up> __il,
124 : _M_payload(__il, std::forward<_Args>(__args)...),
128 // Constructor used by _Optional_base copy constructor when the
129 // contained value is not trivially copy constructible.
131 _Optional_payload_base(bool __engaged,
132 const _Optional_payload_base& __other)
134 if (__other._M_engaged)
135 this->_M_construct(__other._M_get());
138 // Constructor used by _Optional_base move constructor when the
139 // contained value is not trivially move constructible.
141 _Optional_payload_base(bool __engaged,
142 _Optional_payload_base&& __other)
144 if (__other._M_engaged)
145 this->_M_construct(std::move(__other._M_get()));
148 // Copy constructor is only used to when the contained value is
149 // trivially copy constructible.
150 _Optional_payload_base(const _Optional_payload_base&) = default;
152 // Move constructor is only used to when the contained value is
153 // trivially copy constructible.
154 _Optional_payload_base(_Optional_payload_base&&) = default;
156 _Optional_payload_base&
157 operator=(const _Optional_payload_base&) = default;
159 _Optional_payload_base&
160 operator=(_Optional_payload_base&&) = default;
162 // used to perform non-trivial copy assignment.
164 _M_copy_assign(const _Optional_payload_base& __other)
166 if (this->_M_engaged && __other._M_engaged)
167 this->_M_get() = __other._M_get();
170 if (__other._M_engaged)
171 this->_M_construct(__other._M_get());
177 // used to perform non-trivial move assignment.
179 _M_move_assign(_Optional_payload_base&& __other)
180 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
181 is_nothrow_move_assignable<_Tp>>)
183 if (this->_M_engaged && __other._M_engaged)
184 this->_M_get() = std::move(__other._M_get());
187 if (__other._M_engaged)
188 this->_M_construct(std::move(__other._M_get()));
194 struct _Empty_byte { };
196 template<typename _Up, bool = is_trivially_destructible_v<_Up>>
199 constexpr _Storage() noexcept : _M_empty() { }
201 template<typename... _Args>
203 _Storage(in_place_t, _Args&&... __args)
204 : _M_value(std::forward<_Args>(__args)...)
207 template<typename _Vp, typename... _Args>
209 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
210 : _M_value(__il, std::forward<_Args>(__args)...)
213 _Empty_byte _M_empty;
217 template<typename _Up>
218 union _Storage<_Up, false>
220 constexpr _Storage() noexcept : _M_empty() { }
222 template<typename... _Args>
224 _Storage(in_place_t, _Args&&... __args)
225 : _M_value(std::forward<_Args>(__args)...)
228 template<typename _Vp, typename... _Args>
230 _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
231 : _M_value(__il, std::forward<_Args>(__args)...)
234 // User-provided destructor is needed when _Up has non-trivial dtor.
237 _Empty_byte _M_empty;
241 _Storage<_Stored_type> _M_payload;
243 bool _M_engaged = false;
245 template<typename... _Args>
247 _M_construct(_Args&&... __args)
248 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
250 ::new ((void *) std::__addressof(this->_M_payload))
251 _Stored_type(std::forward<_Args>(__args)...);
252 this->_M_engaged = true;
256 _M_destroy() noexcept
259 _M_payload._M_value.~_Stored_type();
262 // The _M_get() operations have _M_engaged as a precondition.
263 // They exist to access the contained value with the appropriate
264 // const-qualification, because _M_payload has had the const removed.
268 { return this->_M_payload._M_value; }
271 _M_get() const noexcept
272 { return this->_M_payload._M_value; }
274 // _M_reset is a 'safe' operation with no precondition.
278 if (this->_M_engaged)
283 // Class template that manages the payload for optionals.
284 template <typename _Tp,
285 bool /*_HasTrivialDestructor*/ =
286 is_trivially_destructible_v<_Tp>,
287 bool /*_HasTrivialCopy */ =
288 is_trivially_copy_assignable_v<_Tp>
289 && is_trivially_copy_constructible_v<_Tp>,
290 bool /*_HasTrivialMove */ =
291 is_trivially_move_assignable_v<_Tp>
292 && is_trivially_move_constructible_v<_Tp>>
293 struct _Optional_payload;
295 // Payload for potentially-constexpr optionals (trivial copy/move/destroy).
296 template <typename _Tp>
297 struct _Optional_payload<_Tp, true, true, true>
298 : _Optional_payload_base<_Tp>
300 using _Optional_payload_base<_Tp>::_Optional_payload_base;
302 _Optional_payload() = default;
305 // Payload for optionals with non-trivial copy construction/assignment.
306 template <typename _Tp>
307 struct _Optional_payload<_Tp, true, false, true>
308 : _Optional_payload_base<_Tp>
310 using _Optional_payload_base<_Tp>::_Optional_payload_base;
312 _Optional_payload() = default;
313 ~_Optional_payload() = default;
314 _Optional_payload(const _Optional_payload&) = default;
315 _Optional_payload(_Optional_payload&&) = default;
316 _Optional_payload& operator=(_Optional_payload&&) = default;
318 // Non-trivial copy assignment.
321 operator=(const _Optional_payload& __other)
323 this->_M_copy_assign(__other);
328 // Payload for optionals with non-trivial move construction/assignment.
329 template <typename _Tp>
330 struct _Optional_payload<_Tp, true, true, false>
331 : _Optional_payload_base<_Tp>
333 using _Optional_payload_base<_Tp>::_Optional_payload_base;
335 _Optional_payload() = default;
336 ~_Optional_payload() = default;
337 _Optional_payload(const _Optional_payload&) = default;
338 _Optional_payload(_Optional_payload&&) = default;
339 _Optional_payload& operator=(const _Optional_payload&) = default;
341 // Non-trivial move assignment.
344 operator=(_Optional_payload&& __other)
345 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
346 is_nothrow_move_assignable<_Tp>>)
348 this->_M_move_assign(std::move(__other));
353 // Payload for optionals with non-trivial copy and move assignment.
354 template <typename _Tp>
355 struct _Optional_payload<_Tp, true, false, false>
356 : _Optional_payload_base<_Tp>
358 using _Optional_payload_base<_Tp>::_Optional_payload_base;
360 _Optional_payload() = default;
361 ~_Optional_payload() = default;
362 _Optional_payload(const _Optional_payload&) = default;
363 _Optional_payload(_Optional_payload&&) = default;
365 // Non-trivial copy assignment.
368 operator=(const _Optional_payload& __other)
370 this->_M_copy_assign(__other);
374 // Non-trivial move assignment.
377 operator=(_Optional_payload&& __other)
378 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
379 is_nothrow_move_assignable<_Tp>>)
381 this->_M_move_assign(std::move(__other));
386 // Payload for optionals with non-trivial destructors.
387 template <typename _Tp, bool _Copy, bool _Move>
388 struct _Optional_payload<_Tp, false, _Copy, _Move>
389 : _Optional_payload<_Tp, true, false, false>
391 // Base class implements all the constructors and assignment operators:
392 using _Optional_payload<_Tp, true, false, false>::_Optional_payload;
393 _Optional_payload() = default;
394 _Optional_payload(const _Optional_payload&) = default;
395 _Optional_payload(_Optional_payload&&) = default;
396 _Optional_payload& operator=(const _Optional_payload&) = default;
397 _Optional_payload& operator=(_Optional_payload&&) = default;
399 // Destructor needs to destroy the contained value:
400 ~_Optional_payload() { this->_M_reset(); }
403 // Common base class for _Optional_base<T> to avoid repeating these
404 // member functions in each specialization.
405 template<typename _Tp, typename _Dp>
406 class _Optional_base_impl
409 using _Stored_type = remove_const_t<_Tp>;
411 // The _M_construct operation has !_M_engaged as a precondition
412 // while _M_destruct has _M_engaged as a precondition.
413 template<typename... _Args>
415 _M_construct(_Args&&... __args)
416 noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
419 (std::__addressof(static_cast<_Dp*>(this)->_M_payload._M_payload))
420 _Stored_type(std::forward<_Args>(__args)...);
421 static_cast<_Dp*>(this)->_M_payload._M_engaged = true;
425 _M_destruct() noexcept
426 { static_cast<_Dp*>(this)->_M_payload._M_destroy(); }
428 // _M_reset is a 'safe' operation with no precondition.
431 { static_cast<_Dp*>(this)->_M_payload._M_reset(); }
433 constexpr bool _M_is_engaged() const noexcept
434 { return static_cast<const _Dp*>(this)->_M_payload._M_engaged; }
436 // The _M_get operations have _M_engaged as a precondition.
440 __glibcxx_assert(this->_M_is_engaged());
441 return static_cast<_Dp*>(this)->_M_payload._M_get();
445 _M_get() const noexcept
447 __glibcxx_assert(this->_M_is_engaged());
448 return static_cast<const _Dp*>(this)->_M_payload._M_get();
453 * @brief Class template that provides copy/move constructors of optional.
455 * Such a separate base class template is necessary in order to
456 * conditionally make copy/move constructors trivial.
458 * When the contained value is trivially copy/move constructible,
459 * the copy/move constructors of _Optional_base will invoke the
460 * trivial copy/move constructor of _Optional_payload. Otherwise,
461 * they will invoke _Optional_payload(bool, const _Optional_payload&)
462 * or _Optional_payload(bool, _Optional_payload&&) to initialize
463 * the contained value, if copying/moving an engaged optional.
465 * Whether the other special members are trivial is determined by the
466 * _Optional_payload<_Tp> specialization used for the _M_payload member.
468 * @see optional, _Enable_special_members
470 template<typename _Tp,
471 bool = is_trivially_copy_constructible_v<_Tp>,
472 bool = is_trivially_move_constructible_v<_Tp>>
473 struct _Optional_base
474 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
476 // Constructors for disengaged optionals.
477 constexpr _Optional_base() = default;
479 // Constructors for engaged optionals.
480 template<typename... _Args,
481 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
483 _Optional_base(in_place_t, _Args&&... __args)
484 : _M_payload(in_place, std::forward<_Args>(__args)...)
487 template<typename _Up, typename... _Args,
488 enable_if_t<is_constructible_v<_Tp,
489 initializer_list<_Up>&,
490 _Args...>, bool> = false>
492 _Optional_base(in_place_t,
493 initializer_list<_Up> __il,
495 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
498 // Copy and move constructors.
500 _Optional_base(const _Optional_base& __other)
501 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
505 _Optional_base(_Optional_base&& __other)
506 noexcept(is_nothrow_move_constructible_v<_Tp>)
507 : _M_payload(__other._M_payload._M_engaged,
508 std::move(__other._M_payload))
511 // Assignment operators.
512 _Optional_base& operator=(const _Optional_base&) = default;
513 _Optional_base& operator=(_Optional_base&&) = default;
515 _Optional_payload<_Tp> _M_payload;
518 template<typename _Tp>
519 struct _Optional_base<_Tp, false, true>
520 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
522 // Constructors for disengaged optionals.
523 constexpr _Optional_base() = default;
525 // Constructors for engaged optionals.
526 template<typename... _Args,
527 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
529 _Optional_base(in_place_t, _Args&&... __args)
530 : _M_payload(in_place, std::forward<_Args>(__args)...)
533 template<typename _Up, typename... _Args,
534 enable_if_t<is_constructible_v<_Tp,
535 initializer_list<_Up>&,
536 _Args...>, bool> = false>
538 _Optional_base(in_place_t,
539 initializer_list<_Up> __il,
541 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
544 // Copy and move constructors.
545 constexpr _Optional_base(const _Optional_base& __other)
546 : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
549 constexpr _Optional_base(_Optional_base&& __other) = default;
551 // Assignment operators.
552 _Optional_base& operator=(const _Optional_base&) = default;
553 _Optional_base& operator=(_Optional_base&&) = default;
555 _Optional_payload<_Tp> _M_payload;
558 template<typename _Tp>
559 struct _Optional_base<_Tp, true, false>
560 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
562 // Constructors for disengaged optionals.
563 constexpr _Optional_base() = default;
565 // Constructors for engaged optionals.
566 template<typename... _Args,
567 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
569 _Optional_base(in_place_t, _Args&&... __args)
570 : _M_payload(in_place, std::forward<_Args>(__args)...)
573 template<typename _Up, typename... _Args,
574 enable_if_t<is_constructible_v<_Tp,
575 initializer_list<_Up>&,
576 _Args...>, bool> = false>
578 _Optional_base(in_place_t,
579 initializer_list<_Up> __il,
581 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
584 // Copy and move constructors.
585 constexpr _Optional_base(const _Optional_base& __other) = default;
588 _Optional_base(_Optional_base&& __other)
589 noexcept(is_nothrow_move_constructible_v<_Tp>)
590 : _M_payload(__other._M_payload._M_engaged,
591 std::move(__other._M_payload))
594 // Assignment operators.
595 _Optional_base& operator=(const _Optional_base&) = default;
596 _Optional_base& operator=(_Optional_base&&) = default;
598 _Optional_payload<_Tp> _M_payload;
601 template<typename _Tp>
602 struct _Optional_base<_Tp, true, true>
603 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
605 // Constructors for disengaged optionals.
606 constexpr _Optional_base() = default;
608 // Constructors for engaged optionals.
609 template<typename... _Args,
610 enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
612 _Optional_base(in_place_t, _Args&&... __args)
613 : _M_payload(in_place, std::forward<_Args>(__args)...)
616 template<typename _Up, typename... _Args,
617 enable_if_t<is_constructible_v<_Tp,
618 initializer_list<_Up>&,
619 _Args...>, bool> = false>
621 _Optional_base(in_place_t,
622 initializer_list<_Up> __il,
624 : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
627 // Copy and move constructors.
628 constexpr _Optional_base(const _Optional_base& __other) = default;
629 constexpr _Optional_base(_Optional_base&& __other) = default;
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>
641 template<typename _Tp, typename _Up>
642 using __converts_from_optional =
643 __or_<is_constructible<_Tp, const optional<_Up>&>,
644 is_constructible<_Tp, optional<_Up>&>,
645 is_constructible<_Tp, const optional<_Up>&&>,
646 is_constructible<_Tp, optional<_Up>&&>,
647 is_convertible<const optional<_Up>&, _Tp>,
648 is_convertible<optional<_Up>&, _Tp>,
649 is_convertible<const optional<_Up>&&, _Tp>,
650 is_convertible<optional<_Up>&&, _Tp>>;
652 template<typename _Tp, typename _Up>
653 using __assigns_from_optional =
654 __or_<is_assignable<_Tp&, const optional<_Up>&>,
655 is_assignable<_Tp&, optional<_Up>&>,
656 is_assignable<_Tp&, const optional<_Up>&&>,
657 is_assignable<_Tp&, optional<_Up>&&>>;
660 * @brief Class template for optional values.
662 template<typename _Tp>
664 : private _Optional_base<_Tp>,
665 private _Enable_copy_move<
667 is_copy_constructible_v<_Tp>,
669 __and_v<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>,
671 is_move_constructible_v<_Tp>,
673 __and_v<is_move_constructible<_Tp>, is_move_assignable<_Tp>>,
677 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
678 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
679 static_assert(!is_reference_v<_Tp>);
682 using _Base = _Optional_base<_Tp>;
685 template<typename _Up>
686 using __not_self = __not_<is_same<optional, __remove_cvref_t<_Up>>>;
687 template<typename _Up>
688 using __not_tag = __not_<is_same<in_place_t, __remove_cvref_t<_Up>>>;
689 template<typename... _Cond>
690 using _Requires = enable_if_t<__and_v<_Cond...>, bool>;
693 using value_type = _Tp;
695 constexpr optional() noexcept { }
697 constexpr optional(nullopt_t) noexcept { }
699 // Converting constructors for engaged optionals.
700 template<typename _Up = _Tp,
701 _Requires<__not_self<_Up>, __not_tag<_Up>,
702 is_constructible<_Tp, _Up>,
703 is_convertible<_Up, _Tp>> = true>
706 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
707 : _Base(std::in_place, std::forward<_Up>(__t)) { }
709 template<typename _Up = _Tp,
710 _Requires<__not_self<_Up>, __not_tag<_Up>,
711 is_constructible<_Tp, _Up>,
712 __not_<is_convertible<_Up, _Tp>>> = false>
715 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
716 : _Base(std::in_place, std::forward<_Up>(__t)) { }
718 template<typename _Up,
719 _Requires<__not_<is_same<_Tp, _Up>>,
720 is_constructible<_Tp, const _Up&>,
721 is_convertible<const _Up&, _Tp>,
722 __not_<__converts_from_optional<_Tp, _Up>>> = true>
724 optional(const optional<_Up>& __t)
725 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
731 template<typename _Up,
732 _Requires<__not_<is_same<_Tp, _Up>>,
733 is_constructible<_Tp, const _Up&>,
734 __not_<is_convertible<const _Up&, _Tp>>,
735 __not_<__converts_from_optional<_Tp, _Up>>> = false>
737 optional(const optional<_Up>& __t)
738 noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
744 template<typename _Up,
745 _Requires<__not_<is_same<_Tp, _Up>>,
746 is_constructible<_Tp, _Up>,
747 is_convertible<_Up, _Tp>,
748 __not_<__converts_from_optional<_Tp, _Up>>> = true>
750 optional(optional<_Up>&& __t)
751 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
754 emplace(std::move(*__t));
757 template<typename _Up,
758 _Requires<__not_<is_same<_Tp, _Up>>,
759 is_constructible<_Tp, _Up>,
760 __not_<is_convertible<_Up, _Tp>>,
761 __not_<__converts_from_optional<_Tp, _Up>>> = false>
763 optional(optional<_Up>&& __t)
764 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
767 emplace(std::move(*__t));
770 template<typename... _Args,
771 _Requires<is_constructible<_Tp, _Args...>> = false>
773 optional(in_place_t, _Args&&... __args)
774 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
775 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
777 template<typename _Up, typename... _Args,
778 _Requires<is_constructible<_Tp,
779 initializer_list<_Up>&,
782 optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
783 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
785 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
788 // Assignment operators.
790 operator=(nullopt_t) noexcept
796 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>
816 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
817 is_constructible<_Tp, const _Up&>,
818 is_assignable<_Tp&, _Up>,
819 __not_<__converts_from_optional<_Tp, _Up>>,
820 __not_<__assigns_from_optional<_Tp, _Up>>>,
822 operator=(const optional<_Up>& __u)
823 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
824 is_nothrow_assignable<_Tp&, const _Up&>>)
828 if (this->_M_is_engaged())
829 this->_M_get() = *__u;
831 this->_M_construct(*__u);
840 template<typename _Up>
841 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
842 is_constructible<_Tp, _Up>,
843 is_assignable<_Tp&, _Up>,
844 __not_<__converts_from_optional<_Tp, _Up>>,
845 __not_<__assigns_from_optional<_Tp, _Up>>>,
847 operator=(optional<_Up>&& __u)
848 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
849 is_nothrow_assignable<_Tp&, _Up>>)
853 if (this->_M_is_engaged())
854 this->_M_get() = std::move(*__u);
856 this->_M_construct(std::move(*__u));
866 template<typename... _Args>
867 enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
868 emplace(_Args&&... __args)
869 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
872 this->_M_construct(std::forward<_Args>(__args)...);
873 return this->_M_get();
876 template<typename _Up, typename... _Args>
877 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
879 emplace(initializer_list<_Up> __il, _Args&&... __args)
880 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
884 this->_M_construct(__il, std::forward<_Args>(__args)...);
885 return this->_M_get();
888 // Destructor is implicit, implemented in _Optional_base.
892 swap(optional& __other)
893 noexcept(is_nothrow_move_constructible_v<_Tp>
894 && is_nothrow_swappable_v<_Tp>)
898 if (this->_M_is_engaged() && __other._M_is_engaged())
899 swap(this->_M_get(), __other._M_get());
900 else if (this->_M_is_engaged())
902 __other._M_construct(std::move(this->_M_get()));
905 else if (__other._M_is_engaged())
907 this->_M_construct(std::move(__other._M_get()));
908 __other._M_destruct();
914 operator->() const noexcept
915 { return std::__addressof(this->_M_get()); }
918 operator->() noexcept
919 { return std::__addressof(this->_M_get()); }
922 operator*() const& noexcept
923 { return this->_M_get(); }
926 operator*()& noexcept
927 { return this->_M_get(); }
930 operator*()&& noexcept
931 { return std::move(this->_M_get()); }
933 constexpr const _Tp&&
934 operator*() const&& noexcept
935 { return std::move(this->_M_get()); }
937 constexpr explicit operator bool() const noexcept
938 { return this->_M_is_engaged(); }
940 constexpr bool has_value() const noexcept
941 { return this->_M_is_engaged(); }
946 return this->_M_is_engaged()
948 : (__throw_bad_optional_access(), this->_M_get());
954 return this->_M_is_engaged()
956 : (__throw_bad_optional_access(), this->_M_get());
962 return this->_M_is_engaged()
963 ? std::move(this->_M_get())
964 : (__throw_bad_optional_access(), std::move(this->_M_get()));
967 constexpr const _Tp&&
970 return this->_M_is_engaged()
971 ? std::move(this->_M_get())
972 : (__throw_bad_optional_access(), std::move(this->_M_get()));
975 template<typename _Up>
977 value_or(_Up&& __u) const&
979 static_assert(is_copy_constructible_v<_Tp>);
980 static_assert(is_convertible_v<_Up&&, _Tp>);
982 return this->_M_is_engaged()
983 ? this->_M_get() : static_cast<_Tp>(std::forward<_Up>(__u));
986 template<typename _Up>
988 value_or(_Up&& __u) &&
990 static_assert(is_move_constructible_v<_Tp>);
991 static_assert(is_convertible_v<_Up&&, _Tp>);
993 return this->_M_is_engaged()
994 ? std::move(this->_M_get())
995 : static_cast<_Tp>(std::forward<_Up>(__u));
998 void reset() noexcept { this->_M_reset(); }
1001 template<typename _Tp>
1002 using __optional_relop_t =
1003 enable_if_t<is_convertible<_Tp, bool>::value, bool>;
1005 // Comparisons between optional values.
1006 template<typename _Tp, typename _Up>
1008 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1009 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
1011 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
1012 && (!__lhs || *__lhs == *__rhs);
1015 template<typename _Tp, typename _Up>
1017 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1018 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
1020 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
1021 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
1024 template<typename _Tp, typename _Up>
1026 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1027 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
1029 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
1032 template<typename _Tp, typename _Up>
1034 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1035 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
1037 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
1040 template<typename _Tp, typename _Up>
1042 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1043 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
1045 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
1048 template<typename _Tp, typename _Up>
1050 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1051 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
1053 return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
1056 #ifdef __cpp_lib_three_way_comparison
1057 template<typename _Tp, three_way_comparable_with<_Tp> _Up>
1058 constexpr compare_three_way_result_t<_Tp, _Up>
1059 operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
1061 return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
1065 // Comparisons with nullopt.
1066 template<typename _Tp>
1068 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
1071 #ifdef __cpp_lib_three_way_comparison
1072 template<typename _Tp>
1073 constexpr strong_ordering
1074 operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
1075 { return bool(__x) <=> false; }
1077 template<typename _Tp>
1079 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
1082 template<typename _Tp>
1084 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1085 { return static_cast<bool>(__lhs); }
1087 template<typename _Tp>
1089 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1090 { return static_cast<bool>(__rhs); }
1092 template<typename _Tp>
1094 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1097 template<typename _Tp>
1099 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
1100 { return static_cast<bool>(__rhs); }
1102 template<typename _Tp>
1104 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
1105 { return static_cast<bool>(__lhs); }
1107 template<typename _Tp>
1109 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1112 template<typename _Tp>
1114 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1117 template<typename _Tp>
1119 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1122 template<typename _Tp>
1124 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1127 template<typename _Tp>
1129 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1131 #endif // three-way-comparison
1133 // Comparisons with value type.
1134 template<typename _Tp, typename _Up>
1136 operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
1137 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
1138 { return __lhs && *__lhs == __rhs; }
1140 template<typename _Tp, typename _Up>
1142 operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
1143 -> __optional_relop_t<decltype(declval<_Up>() == declval<_Tp>())>
1144 { return __rhs && __lhs == *__rhs; }
1146 template<typename _Tp, typename _Up>
1148 operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
1149 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
1150 { return !__lhs || *__lhs != __rhs; }
1152 template<typename _Tp, typename _Up>
1154 operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
1155 -> __optional_relop_t<decltype(declval<_Up>() != declval<_Tp>())>
1156 { return !__rhs || __lhs != *__rhs; }
1158 template<typename _Tp, typename _Up>
1160 operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
1161 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
1162 { return !__lhs || *__lhs < __rhs; }
1164 template<typename _Tp, typename _Up>
1166 operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
1167 -> __optional_relop_t<decltype(declval<_Up>() < declval<_Tp>())>
1168 { return __rhs && __lhs < *__rhs; }
1170 template<typename _Tp, typename _Up>
1172 operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
1173 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
1174 { return __lhs && *__lhs > __rhs; }
1176 template<typename _Tp, typename _Up>
1178 operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
1179 -> __optional_relop_t<decltype(declval<_Up>() > declval<_Tp>())>
1180 { return !__rhs || __lhs > *__rhs; }
1182 template<typename _Tp, typename _Up>
1184 operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
1185 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
1186 { return !__lhs || *__lhs <= __rhs; }
1188 template<typename _Tp, typename _Up>
1190 operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
1191 -> __optional_relop_t<decltype(declval<_Up>() <= declval<_Tp>())>
1192 { return __rhs && __lhs <= *__rhs; }
1194 template<typename _Tp, typename _Up>
1196 operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
1197 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
1198 { return __lhs && *__lhs >= __rhs; }
1200 template<typename _Tp, typename _Up>
1202 operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
1203 -> __optional_relop_t<decltype(declval<_Up>() >= declval<_Tp>())>
1204 { return !__rhs || __lhs >= *__rhs; }
1206 #ifdef __cpp_lib_three_way_comparison
1207 template<typename _Tp, typename _Up>
1208 constexpr compare_three_way_result_t<_Tp, _Up>
1209 operator<=>(const optional<_Tp>& __x, const _Up& __v)
1210 { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
1213 // Swap and creation functions.
1215 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1216 // 2748. swappable traits for optionals
1217 template<typename _Tp>
1218 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
1219 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
1220 noexcept(noexcept(__lhs.swap(__rhs)))
1221 { __lhs.swap(__rhs); }
1223 template<typename _Tp>
1224 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
1225 swap(optional<_Tp>&, optional<_Tp>&) = delete;
1227 template<typename _Tp>
1229 enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>,
1230 optional<decay_t<_Tp>>>
1231 make_optional(_Tp&& __t)
1232 noexcept(is_nothrow_constructible_v<optional<decay_t<_Tp>>, _Tp>)
1233 { return optional<decay_t<_Tp>>{ std::forward<_Tp>(__t) }; }
1235 template<typename _Tp, typename... _Args>
1237 enable_if_t<is_constructible_v<_Tp, _Args...>,
1239 make_optional(_Args&&... __args)
1240 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1241 { return optional<_Tp>{ in_place, std::forward<_Args>(__args)... }; }
1243 template<typename _Tp, typename _Up, typename... _Args>
1245 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1247 make_optional(initializer_list<_Up> __il, _Args&&... __args)
1248 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1249 { return optional<_Tp>{ in_place, __il, std::forward<_Args>(__args)... }; }
1253 template<typename _Tp, typename _Up = remove_const_t<_Tp>,
1254 bool = __poison_hash<_Up>::__enable_hash_call>
1255 struct __optional_hash_call_base
1258 operator()(const optional<_Tp>& __t) const
1259 noexcept(noexcept(hash<_Up>{}(*__t)))
1261 // We pick an arbitrary hash for disengaged optionals which hopefully
1262 // usual values of _Tp won't typically hash to.
1263 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1264 return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
1268 template<typename _Tp, typename _Up>
1269 struct __optional_hash_call_base<_Tp, _Up, false> {};
1271 template<typename _Tp>
1272 struct hash<optional<_Tp>>
1273 : private __poison_hash<remove_const_t<_Tp>>,
1274 public __optional_hash_call_base<_Tp>
1276 using result_type [[__deprecated__]] = size_t;
1277 using argument_type [[__deprecated__]] = optional<_Tp>;
1280 template<typename _Tp>
1281 struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
1286 #if __cpp_deduction_guides >= 201606
1287 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1290 _GLIBCXX_END_NAMESPACE_VERSION
1295 #endif // _GLIBCXX_OPTIONAL