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() { }
89 virtual const char* what() const noexcept override
90 { return "bad optional access"; }
92 virtual ~bad_optional_access() noexcept = default;
96 __throw_bad_optional_access()
97 __attribute__((__noreturn__));
99 // XXX Does not belong here.
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.
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 ::new ((void *) std::__addressof(this->_M_payload))
252 _Stored_type(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 ~_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...>)
420 (std::__addressof(static_cast<_Dp*>(this)->_M_payload._M_payload))
421 _Stored_type(std::forward<_Args>(__args)...);
422 static_cast<_Dp*>(this)->_M_payload._M_engaged = true;
426 _M_destruct() noexcept
427 { static_cast<_Dp*>(this)->_M_payload._M_destroy(); }
429 // _M_reset is a 'safe' operation with no precondition.
432 { static_cast<_Dp*>(this)->_M_payload._M_reset(); }
434 constexpr bool _M_is_engaged() const noexcept
435 { return static_cast<const _Dp*>(this)->_M_payload._M_engaged; }
437 // The _M_get operations have _M_engaged as a precondition.
441 __glibcxx_assert(this->_M_is_engaged());
442 return static_cast<_Dp*>(this)->_M_payload._M_get();
446 _M_get() const noexcept
448 __glibcxx_assert(this->_M_is_engaged());
449 return static_cast<const _Dp*>(this)->_M_payload._M_get();
454 * @brief Class template that provides copy/move constructors of optional.
456 * Such a separate base class template is necessary in order to
457 * conditionally make copy/move constructors trivial.
459 * When the contained value is trivially copy/move constructible,
460 * the copy/move constructors of _Optional_base will invoke the
461 * trivial copy/move constructor of _Optional_payload. Otherwise,
462 * they will invoke _Optional_payload(bool, const _Optional_payload&)
463 * or _Optional_payload(bool, _Optional_payload&&) to initialize
464 * the contained value, if copying/moving an engaged optional.
466 * Whether the other special members are trivial is determined by the
467 * _Optional_payload<_Tp> specialization used for the _M_payload member.
469 * @see optional, _Enable_special_members
471 template<typename _Tp,
472 bool = is_trivially_copy_constructible_v<_Tp>,
473 bool = is_trivially_move_constructible_v<_Tp>>
474 struct _Optional_base
475 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
477 // Constructors for disengaged optionals.
478 constexpr _Optional_base() = default;
480 // Constructors for engaged optionals.
481 template<typename... _Args,
482 enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
483 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
484 : _M_payload(in_place,
485 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>
491 constexpr explicit _Optional_base(in_place_t,
492 initializer_list<_Up> __il,
494 : _M_payload(in_place,
495 __il, std::forward<_Args>(__args)...)
498 // Copy and move constructors.
499 constexpr _Optional_base(const _Optional_base& __other)
500 : _M_payload(__other._M_payload._M_engaged,
504 constexpr _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>
527 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
528 : _M_payload(in_place,
529 std::forward<_Args>(__args)...) { }
531 template<typename _Up, typename... _Args,
532 enable_if_t<is_constructible_v<_Tp,
533 initializer_list<_Up>&,
534 _Args&&...>, bool> = false>
535 constexpr explicit _Optional_base(in_place_t,
536 initializer_list<_Up> __il,
538 : _M_payload(in_place,
539 __il, std::forward<_Args>(__args)...)
542 // Copy and move constructors.
543 constexpr _Optional_base(const _Optional_base& __other)
544 : _M_payload(__other._M_payload._M_engaged,
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>
567 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
568 : _M_payload(in_place,
569 std::forward<_Args>(__args)...) { }
571 template<typename _Up, typename... _Args,
572 enable_if_t<is_constructible_v<_Tp,
573 initializer_list<_Up>&,
574 _Args&&...>, bool> = false>
575 constexpr explicit _Optional_base(in_place_t,
576 initializer_list<_Up> __il,
578 : _M_payload(in_place,
579 __il, std::forward<_Args>(__args)...)
582 // Copy and move constructors.
583 constexpr _Optional_base(const _Optional_base& __other) = default;
585 constexpr _Optional_base(_Optional_base&& __other)
586 noexcept(is_nothrow_move_constructible_v<_Tp>)
587 : _M_payload(__other._M_payload._M_engaged,
588 std::move(__other._M_payload))
591 // Assignment operators.
592 _Optional_base& operator=(const _Optional_base&) = default;
593 _Optional_base& operator=(_Optional_base&&) = default;
595 _Optional_payload<_Tp> _M_payload;
598 template<typename _Tp>
599 struct _Optional_base<_Tp, true, true>
600 : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
602 // Constructors for disengaged optionals.
603 constexpr _Optional_base() = default;
605 // Constructors for engaged optionals.
606 template<typename... _Args,
607 enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
608 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
609 : _M_payload(in_place,
610 std::forward<_Args>(__args)...) { }
612 template<typename _Up, typename... _Args,
613 enable_if_t<is_constructible_v<_Tp,
614 initializer_list<_Up>&,
615 _Args&&...>, bool> = false>
616 constexpr explicit _Optional_base(in_place_t,
617 initializer_list<_Up> __il,
619 : _M_payload(in_place,
620 __il, std::forward<_Args>(__args)...)
623 // Copy and move constructors.
624 constexpr _Optional_base(const _Optional_base& __other) = default;
625 constexpr _Optional_base(_Optional_base&& __other) = default;
627 // Assignment operators.
628 _Optional_base& operator=(const _Optional_base&) = default;
629 _Optional_base& operator=(_Optional_base&&) = default;
631 _Optional_payload<_Tp> _M_payload;
634 template<typename _Tp>
637 template<typename _Tp, typename _Up>
638 using __converts_from_optional =
639 __or_<is_constructible<_Tp, const optional<_Up>&>,
640 is_constructible<_Tp, optional<_Up>&>,
641 is_constructible<_Tp, const optional<_Up>&&>,
642 is_constructible<_Tp, optional<_Up>&&>,
643 is_convertible<const optional<_Up>&, _Tp>,
644 is_convertible<optional<_Up>&, _Tp>,
645 is_convertible<const optional<_Up>&&, _Tp>,
646 is_convertible<optional<_Up>&&, _Tp>>;
648 template<typename _Tp, typename _Up>
649 using __assigns_from_optional =
650 __or_<is_assignable<_Tp&, const optional<_Up>&>,
651 is_assignable<_Tp&, optional<_Up>&>,
652 is_assignable<_Tp&, const optional<_Up>&&>,
653 is_assignable<_Tp&, optional<_Up>&&>>;
656 * @brief Class template for optional values.
658 template<typename _Tp>
660 : private _Optional_base<_Tp>,
661 private _Enable_copy_move<
663 is_copy_constructible_v<_Tp>,
665 __and_v<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>,
667 is_move_constructible_v<_Tp>,
669 __and_v<is_move_constructible<_Tp>, is_move_assignable<_Tp>>,
673 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
674 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
675 static_assert(!is_reference_v<_Tp>);
678 using _Base = _Optional_base<_Tp>;
681 template<typename _Up>
682 using __not_self = __not_<is_same<optional, __remove_cvref_t<_Up>>>;
683 template<typename _Up>
684 using __not_tag = __not_<is_same<in_place_t, __remove_cvref_t<_Up>>>;
685 template<typename... _Cond>
686 using _Requires = enable_if_t<__and_v<_Cond...>, bool>;
689 using value_type = _Tp;
691 constexpr optional() = default;
693 constexpr optional(nullopt_t) noexcept { }
695 // Converting constructors for engaged optionals.
696 template<typename _Up = _Tp,
697 _Requires<__not_self<_Up>, __not_tag<_Up>,
698 is_constructible<_Tp, _Up&&>,
699 is_convertible<_Up&&, _Tp>> = true>
702 : _Base(std::in_place, std::forward<_Up>(__t)) { }
704 template<typename _Up = _Tp,
705 _Requires<__not_self<_Up>, __not_tag<_Up>,
706 is_constructible<_Tp, _Up&&>,
707 __not_<is_convertible<_Up&&, _Tp>>> = false>
710 : _Base(std::in_place, std::forward<_Up>(__t)) { }
712 template<typename _Up,
713 _Requires<__not_<is_same<_Tp, _Up>>,
714 is_constructible<_Tp, const _Up&>,
715 is_convertible<const _Up&, _Tp>,
716 __not_<__converts_from_optional<_Tp, _Up>>> = true>
718 optional(const optional<_Up>& __t)
724 template<typename _Up,
725 _Requires<__not_<is_same<_Tp, _Up>>,
726 is_constructible<_Tp, const _Up&>,
727 __not_<is_convertible<const _Up&, _Tp>>,
728 __not_<__converts_from_optional<_Tp, _Up>>> = false>
730 optional(const optional<_Up>& __t)
736 template <typename _Up,
737 _Requires<__not_<is_same<_Tp, _Up>>,
738 is_constructible<_Tp, _Up&&>,
739 is_convertible<_Up&&, _Tp>,
740 __not_<__converts_from_optional<_Tp, _Up>>> = true>
742 optional(optional<_Up>&& __t)
745 emplace(std::move(*__t));
748 template <typename _Up,
749 _Requires<__not_<is_same<_Tp, _Up>>,
750 is_constructible<_Tp, _Up&&>,
751 __not_<is_convertible<_Up&&, _Tp>>,
752 __not_<__converts_from_optional<_Tp, _Up>>> = false>
754 optional(optional<_Up>&& __t)
757 emplace(std::move(*__t));
760 template<typename... _Args,
761 _Requires<is_constructible<_Tp, _Args&&...>> = false>
763 optional(in_place_t, _Args&&... __args)
764 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
766 template<typename _Up, typename... _Args,
767 _Requires<is_constructible<_Tp,
768 initializer_list<_Up>&,
769 _Args&&...>> = false>
771 optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
772 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
774 // Assignment operators.
776 operator=(nullopt_t) noexcept
782 template<typename _Up = _Tp>
783 enable_if_t<__and_v<__not_self<_Up>,
784 __not_<__and_<is_scalar<_Tp>,
785 is_same<_Tp, decay_t<_Up>>>>,
786 is_constructible<_Tp, _Up>,
787 is_assignable<_Tp&, _Up>>,
791 if (this->_M_is_engaged())
792 this->_M_get() = std::forward<_Up>(__u);
794 this->_M_construct(std::forward<_Up>(__u));
799 template<typename _Up>
800 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
801 is_constructible<_Tp, const _Up&>,
802 is_assignable<_Tp&, _Up>,
803 __not_<__converts_from_optional<_Tp, _Up>>,
804 __not_<__assigns_from_optional<_Tp, _Up>>>,
806 operator=(const optional<_Up>& __u)
810 if (this->_M_is_engaged())
811 this->_M_get() = *__u;
813 this->_M_construct(*__u);
822 template<typename _Up>
823 enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
824 is_constructible<_Tp, _Up>,
825 is_assignable<_Tp&, _Up>,
826 __not_<__converts_from_optional<_Tp, _Up>>,
827 __not_<__assigns_from_optional<_Tp, _Up>>>,
829 operator=(optional<_Up>&& __u)
833 if (this->_M_is_engaged())
834 this->_M_get() = std::move(*__u);
836 this->_M_construct(std::move(*__u));
846 template<typename... _Args>
847 enable_if_t<is_constructible_v<_Tp, _Args&&...>, _Tp&>
848 emplace(_Args&&... __args)
851 this->_M_construct(std::forward<_Args>(__args)...);
852 return this->_M_get();
855 template<typename _Up, typename... _Args>
856 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&,
858 emplace(initializer_list<_Up> __il, _Args&&... __args)
861 this->_M_construct(__il, std::forward<_Args>(__args)...);
862 return this->_M_get();
865 // Destructor is implicit, implemented in _Optional_base.
869 swap(optional& __other)
870 noexcept(is_nothrow_move_constructible_v<_Tp>
871 && is_nothrow_swappable_v<_Tp>)
875 if (this->_M_is_engaged() && __other._M_is_engaged())
876 swap(this->_M_get(), __other._M_get());
877 else if (this->_M_is_engaged())
879 __other._M_construct(std::move(this->_M_get()));
882 else if (__other._M_is_engaged())
884 this->_M_construct(std::move(__other._M_get()));
885 __other._M_destruct();
892 { return std::__addressof(this->_M_get()); }
896 { return std::__addressof(this->_M_get()); }
900 { return this->_M_get(); }
904 { return this->_M_get(); }
908 { return std::move(this->_M_get()); }
910 constexpr const _Tp&&
912 { return std::move(this->_M_get()); }
914 constexpr explicit operator bool() const noexcept
915 { return this->_M_is_engaged(); }
917 constexpr bool has_value() const noexcept
918 { return this->_M_is_engaged(); }
923 return this->_M_is_engaged()
925 : (__throw_bad_optional_access(), this->_M_get());
931 return this->_M_is_engaged()
933 : (__throw_bad_optional_access(), this->_M_get());
939 return this->_M_is_engaged()
940 ? std::move(this->_M_get())
941 : (__throw_bad_optional_access(), std::move(this->_M_get()));
944 constexpr const _Tp&&
947 return this->_M_is_engaged()
948 ? std::move(this->_M_get())
949 : (__throw_bad_optional_access(), std::move(this->_M_get()));
952 template<typename _Up>
954 value_or(_Up&& __u) const&
956 static_assert(is_copy_constructible_v<_Tp>);
957 static_assert(is_convertible_v<_Up&&, _Tp>);
959 return this->_M_is_engaged()
960 ? this->_M_get() : static_cast<_Tp>(std::forward<_Up>(__u));
963 template<typename _Up>
965 value_or(_Up&& __u) &&
967 static_assert(is_move_constructible_v<_Tp>);
968 static_assert(is_convertible_v<_Up&&, _Tp>);
970 return this->_M_is_engaged()
971 ? std::move(this->_M_get())
972 : static_cast<_Tp>(std::forward<_Up>(__u));
975 void reset() noexcept { this->_M_reset(); }
978 template<typename _Tp>
979 using __optional_relop_t =
980 enable_if_t<is_convertible<_Tp, bool>::value, bool>;
982 // Comparisons between optional values.
983 template<typename _Tp, typename _Up>
985 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
986 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
988 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
989 && (!__lhs || *__lhs == *__rhs);
992 template<typename _Tp, typename _Up>
994 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
995 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
997 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
998 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
1001 template<typename _Tp, typename _Up>
1003 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1004 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
1006 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
1009 template<typename _Tp, typename _Up>
1011 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1012 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
1014 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
1017 template<typename _Tp, typename _Up>
1019 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1020 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
1022 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
1025 template<typename _Tp, typename _Up>
1027 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1028 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
1030 return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
1033 #ifdef __cpp_lib_three_way_comparison
1034 template<typename _Tp, three_way_comparable_with<_Tp> _Up>
1035 constexpr compare_three_way_result_t<_Tp, _Up>
1036 operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
1038 return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
1042 // Comparisons with nullopt.
1043 template<typename _Tp>
1045 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
1048 #ifdef __cpp_lib_three_way_comparison
1049 template<typename _Tp>
1050 constexpr strong_ordering
1051 operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
1052 { return bool(__x) <=> false; }
1054 template<typename _Tp>
1056 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
1059 template<typename _Tp>
1061 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1062 { return static_cast<bool>(__lhs); }
1064 template<typename _Tp>
1066 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1067 { return static_cast<bool>(__rhs); }
1069 template<typename _Tp>
1071 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1074 template<typename _Tp>
1076 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
1077 { return static_cast<bool>(__rhs); }
1079 template<typename _Tp>
1081 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
1082 { return static_cast<bool>(__lhs); }
1084 template<typename _Tp>
1086 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1089 template<typename _Tp>
1091 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1094 template<typename _Tp>
1096 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1099 template<typename _Tp>
1101 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1104 template<typename _Tp>
1106 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1108 #endif // three-way-comparison
1110 // Comparisons with value type.
1111 template<typename _Tp, typename _Up>
1113 operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
1114 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
1115 { return __lhs && *__lhs == __rhs; }
1117 template<typename _Tp, typename _Up>
1119 operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
1120 -> __optional_relop_t<decltype(declval<_Up>() == declval<_Tp>())>
1121 { return __rhs && __lhs == *__rhs; }
1123 template<typename _Tp, typename _Up>
1125 operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
1126 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
1127 { return !__lhs || *__lhs != __rhs; }
1129 template<typename _Tp, typename _Up>
1131 operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
1132 -> __optional_relop_t<decltype(declval<_Up>() != declval<_Tp>())>
1133 { return !__rhs || __lhs != *__rhs; }
1135 template<typename _Tp, typename _Up>
1137 operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
1138 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
1139 { return !__lhs || *__lhs < __rhs; }
1141 template<typename _Tp, typename _Up>
1143 operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
1144 -> __optional_relop_t<decltype(declval<_Up>() < declval<_Tp>())>
1145 { return __rhs && __lhs < *__rhs; }
1147 template<typename _Tp, typename _Up>
1149 operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
1150 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
1151 { return __lhs && *__lhs > __rhs; }
1153 template<typename _Tp, typename _Up>
1155 operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
1156 -> __optional_relop_t<decltype(declval<_Up>() > declval<_Tp>())>
1157 { return !__rhs || __lhs > *__rhs; }
1159 template<typename _Tp, typename _Up>
1161 operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
1162 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
1163 { return !__lhs || *__lhs <= __rhs; }
1165 template<typename _Tp, typename _Up>
1167 operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
1168 -> __optional_relop_t<decltype(declval<_Up>() <= declval<_Tp>())>
1169 { return __rhs && __lhs <= *__rhs; }
1171 template<typename _Tp, typename _Up>
1173 operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
1174 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
1175 { return __lhs && *__lhs >= __rhs; }
1177 template<typename _Tp, typename _Up>
1179 operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
1180 -> __optional_relop_t<decltype(declval<_Up>() >= declval<_Tp>())>
1181 { return !__rhs || __lhs >= *__rhs; }
1183 #ifdef __cpp_lib_three_way_comparison
1184 template<typename _Tp, typename _Up>
1185 constexpr compare_three_way_result_t<_Tp, _Up>
1186 operator<=>(const optional<_Tp>& __x, const _Up& __v)
1187 { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
1190 // Swap and creation functions.
1192 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1193 // 2748. swappable traits for optionals
1194 template<typename _Tp>
1195 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
1196 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
1197 noexcept(noexcept(__lhs.swap(__rhs)))
1198 { __lhs.swap(__rhs); }
1200 template<typename _Tp>
1201 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
1202 swap(optional<_Tp>&, optional<_Tp>&) = delete;
1204 template<typename _Tp>
1205 constexpr optional<decay_t<_Tp>>
1206 make_optional(_Tp&& __t)
1207 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
1209 template<typename _Tp, typename ..._Args>
1210 constexpr optional<_Tp>
1211 make_optional(_Args&&... __args)
1212 { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
1214 template<typename _Tp, typename _Up, typename ..._Args>
1215 constexpr optional<_Tp>
1216 make_optional(initializer_list<_Up> __il, _Args&&... __args)
1217 { return optional<_Tp> { in_place, __il, std::forward<_Args>(__args)... }; }
1221 template<typename _Tp, typename _Up = remove_const_t<_Tp>,
1222 bool = __poison_hash<_Up>::__enable_hash_call>
1223 struct __optional_hash_call_base
1226 operator()(const optional<_Tp>& __t) const
1227 noexcept(noexcept(hash<_Up>{}(*__t)))
1229 // We pick an arbitrary hash for disengaged optionals which hopefully
1230 // usual values of _Tp won't typically hash to.
1231 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1232 return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
1236 template<typename _Tp, typename _Up>
1237 struct __optional_hash_call_base<_Tp, _Up, false> {};
1239 template<typename _Tp>
1240 struct hash<optional<_Tp>>
1241 : private __poison_hash<remove_const_t<_Tp>>,
1242 public __optional_hash_call_base<_Tp>
1244 using result_type [[__deprecated__]] = size_t;
1245 using argument_type [[__deprecated__]] = optional<_Tp>;
1248 template<typename _Tp>
1249 struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
1254 #if __cpp_deduction_guides >= 201606
1255 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1258 _GLIBCXX_END_NAMESPACE_VERSION
1263 #endif // _GLIBCXX_OPTIONAL