1 // <optional> -*- C++ -*-
3 // Copyright (C) 2013-2017 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 #if __cplusplus <= 201402L
33 # include <bits/c++17_warning.h>
37 #include <type_traits>
40 #include <initializer_list>
41 #include <bits/functexcept.h>
42 #include <bits/functional_hash.h>
43 #include <bits/enable_special_members.h>
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 * @addtogroup utilities
54 template<typename _Tp>
57 /// Tag type to disengage optional objects.
60 // Do not user-declare default constructor at all for
61 // optional_value = {} syntax to work.
62 // nullopt_t() = delete;
64 // Used for constructing nullopt.
65 enum class _Construct { _Token };
67 // Must be constexpr for nullopt_t to be literal.
68 explicit constexpr nullopt_t(_Construct) { }
71 /// Tag to disengage optional objects.
72 inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
75 * @brief Exception class thrown when a disengaged optional object is
79 class bad_optional_access : public exception
82 bad_optional_access() { }
83 virtual const char* what() const noexcept override
84 {return "bad optional access";}
86 virtual ~bad_optional_access() noexcept = default;
90 __throw_bad_optional_access()
91 __attribute__((__noreturn__));
93 // XXX Does not belong here.
95 __throw_bad_optional_access()
96 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
99 // Payload for constexpr optionals.
100 template <typename _Tp,
101 bool /*_TrivialCopyMove*/ =
102 is_trivially_copy_constructible<_Tp>::value
103 && is_trivially_move_constructible<_Tp>::value,
104 bool /*_ShouldProvideDestructor*/ =
105 is_trivially_destructible<_Tp>::value>
106 struct _Optional_payload
108 constexpr _Optional_payload()
111 template<typename... _Args>
112 constexpr _Optional_payload(in_place_t, _Args&&... __args)
113 : _M_payload(std::forward<_Args>(__args)...),
117 template<typename _Up, typename... _Args>
118 constexpr _Optional_payload(std::initializer_list<_Up> __il,
120 : _M_payload(__il, std::forward<_Args>(__args)...),
123 template <class _Up> struct __ctor_tag {};
125 constexpr _Optional_payload(__ctor_tag<bool>,
127 : _M_payload(__other),
131 constexpr _Optional_payload(__ctor_tag<void>)
135 constexpr _Optional_payload(__ctor_tag<bool>, _Tp&& __other)
136 : _M_payload(std::move(__other)),
140 constexpr _Optional_payload(bool __engaged,
141 const _Optional_payload& __other)
142 : _Optional_payload(__engaged ?
143 _Optional_payload(__ctor_tag<bool>{},
144 __other._M_payload) :
145 _Optional_payload(__ctor_tag<void>{}))
148 constexpr _Optional_payload(bool __engaged,
149 _Optional_payload&& __other)
150 : _Optional_payload(__engaged
151 ? _Optional_payload(__ctor_tag<bool>{},
152 std::move(__other._M_payload))
153 : _Optional_payload(__ctor_tag<void>{}))
156 using _Stored_type = remove_const_t<_Tp>;
157 struct _Empty_byte { };
159 _Empty_byte _M_empty;
160 _Stored_type _M_payload;
162 bool _M_engaged = false;
165 // Payload for non-constexpr optionals with non-trivial destructor.
166 template <typename _Tp>
167 struct _Optional_payload<_Tp, false, false>
169 constexpr _Optional_payload()
172 template <typename... _Args>
173 constexpr _Optional_payload(in_place_t, _Args&&... __args)
174 : _M_payload(std::forward<_Args>(__args)...),
177 template<typename _Up, typename... _Args>
178 constexpr _Optional_payload(std::initializer_list<_Up> __il,
180 : _M_payload(__il, std::forward<_Args>(__args)...),
183 _Optional_payload(bool __engaged, const _Optional_payload& __other)
184 : _Optional_payload(__other)
188 _Optional_payload(bool __engaged, _Optional_payload&& __other)
189 : _Optional_payload(std::move(__other))
192 constexpr _Optional_payload(const _Optional_payload& __other)
194 if (__other._M_engaged)
195 this->_M_construct(__other._M_payload);
198 constexpr _Optional_payload(_Optional_payload&& __other)
200 if (__other._M_engaged)
201 this->_M_construct(std::move(__other._M_payload));
204 using _Stored_type = remove_const_t<_Tp>;
205 struct _Empty_byte { };
207 _Empty_byte _M_empty;
208 _Stored_type _M_payload;
210 bool _M_engaged = false;
215 _M_payload.~_Stored_type();
218 template<typename... _Args>
220 _M_construct(_Args&&... __args)
221 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
223 ::new ((void *) std::__addressof(this->_M_payload))
224 _Stored_type(std::forward<_Args>(__args)...);
225 this->_M_engaged = true;
229 // Payload for non-constexpr optionals with trivial destructor.
230 template <typename _Tp>
231 struct _Optional_payload<_Tp, false, true>
233 constexpr _Optional_payload()
236 template <typename... _Args>
237 constexpr _Optional_payload(in_place_t, _Args&&... __args)
238 : _M_payload(std::forward<_Args>(__args)...),
241 template<typename _Up, typename... _Args>
242 constexpr _Optional_payload(std::initializer_list<_Up> __il,
244 : _M_payload(__il, std::forward<_Args>(__args)...),
247 _Optional_payload(bool __engaged, const _Optional_payload& __other)
248 : _Optional_payload(__other)
252 _Optional_payload(bool __engaged, _Optional_payload&& __other)
253 : _Optional_payload(std::move(__other))
256 constexpr _Optional_payload(const _Optional_payload& __other)
258 if (__other._M_engaged)
259 this->_M_construct(__other._M_payload);
262 constexpr _Optional_payload(_Optional_payload&& __other)
264 if (__other._M_engaged)
265 this->_M_construct(std::move(__other._M_payload));
268 using _Stored_type = remove_const_t<_Tp>;
269 struct _Empty_byte { };
271 _Empty_byte _M_empty;
272 _Stored_type _M_payload;
274 bool _M_engaged = false;
276 template<typename... _Args>
278 _M_construct(_Args&&... __args)
279 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
281 ::new ((void *) std::__addressof(this->_M_payload))
282 _Stored_type(std::forward<_Args>(__args)...);
283 this->_M_engaged = true;
288 * @brief Class template that holds the necessary state for @ref optional
289 * and that has the responsibility for construction and the special members.
291 * Such a separate base class template is necessary in order to
292 * conditionally enable the special members (e.g. copy/move constructors).
293 * Note that this means that @ref _Optional_base implements the
294 * functionality for copy and move assignment, but not for converting
297 * @see optional, _Enable_special_members
299 template<typename _Tp>
303 // Remove const to avoid prohibition of reusing object storage for
304 // const-qualified types in [3.8/9]. This is strictly internal
305 // and even optional itself is oblivious to it.
306 using _Stored_type = remove_const_t<_Tp>;
310 // Constructors for disengaged optionals.
311 constexpr _Optional_base() noexcept
314 constexpr _Optional_base(nullopt_t) noexcept
317 // Constructors for engaged optionals.
318 template<typename... _Args,
319 enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
320 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
321 : _M_payload(in_place,
322 std::forward<_Args>(__args)...) { }
324 template<typename _Up, typename... _Args,
325 enable_if_t<is_constructible_v<_Tp,
326 initializer_list<_Up>&,
327 _Args&&...>, bool> = false>
328 constexpr explicit _Optional_base(in_place_t,
329 initializer_list<_Up> __il,
331 : _M_payload(in_place,
332 __il, std::forward<_Args>(__args)...)
335 // Copy and move constructors.
336 constexpr _Optional_base(const _Optional_base& __other)
337 : _M_payload(__other._M_payload._M_engaged,
341 constexpr _Optional_base(_Optional_base&& __other)
342 noexcept(is_nothrow_move_constructible<_Tp>())
343 : _M_payload(__other._M_payload._M_engaged,
344 std::move(__other._M_payload))
347 // Assignment operators.
349 operator=(const _Optional_base& __other)
351 if (this->_M_payload._M_engaged && __other._M_payload._M_engaged)
352 this->_M_get() = __other._M_get();
355 if (__other._M_payload._M_engaged)
356 this->_M_construct(__other._M_get());
365 operator=(_Optional_base&& __other)
366 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
367 is_nothrow_move_assignable<_Tp>>())
369 if (this->_M_payload._M_engaged && __other._M_payload._M_engaged)
370 this->_M_get() = std::move(__other._M_get());
373 if (__other._M_payload._M_engaged)
374 this->_M_construct(std::move(__other._M_get()));
380 // The following functionality is also needed by optional, hence the
381 // protected accessibility.
383 constexpr bool _M_is_engaged() const noexcept
384 { return this->_M_payload._M_engaged; }
386 // The _M_get operations have _M_engaged as a precondition.
389 { return this->_M_payload._M_payload; }
392 _M_get() const noexcept
393 { return this->_M_payload._M_payload; }
395 // The _M_construct operation has !_M_engaged as a precondition
396 // while _M_destruct has _M_engaged as a precondition.
397 template<typename... _Args>
399 _M_construct(_Args&&... __args)
400 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
402 ::new (std::__addressof(this->_M_payload._M_payload))
403 _Stored_type(std::forward<_Args>(__args)...);
404 this->_M_payload._M_engaged = true;
410 this->_M_payload._M_engaged = false;
411 this->_M_payload._M_payload.~_Stored_type();
414 // _M_reset is a 'safe' operation with no precondition.
418 if (this->_M_payload._M_engaged)
423 _Optional_payload<_Tp> _M_payload;
426 template<typename _Tp>
429 template<typename _Tp, typename _Up>
430 using __converts_from_optional =
431 __or_<is_constructible<_Tp, const optional<_Up>&>,
432 is_constructible<_Tp, optional<_Up>&>,
433 is_constructible<_Tp, const optional<_Up>&&>,
434 is_constructible<_Tp, optional<_Up>&&>,
435 is_convertible<const optional<_Up>&, _Tp>,
436 is_convertible<optional<_Up>&, _Tp>,
437 is_convertible<const optional<_Up>&&, _Tp>,
438 is_convertible<optional<_Up>&&, _Tp>>;
440 template<typename _Tp, typename _Up>
441 using __assigns_from_optional =
442 __or_<is_assignable<_Tp&, const optional<_Up>&>,
443 is_assignable<_Tp&, optional<_Up>&>,
444 is_assignable<_Tp&, const optional<_Up>&&>,
445 is_assignable<_Tp&, optional<_Up>&&>>;
448 * @brief Class template for optional values.
450 template<typename _Tp>
452 : private _Optional_base<_Tp>,
453 private _Enable_copy_move<
455 is_copy_constructible<_Tp>::value,
457 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
459 is_move_constructible<_Tp>::value,
461 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
465 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
466 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
467 static_assert(!is_reference_v<_Tp>);
470 using _Base = _Optional_base<_Tp>;
473 using value_type = _Tp;
475 constexpr optional() = default;
477 constexpr optional(nullopt_t) noexcept
480 // Converting constructors for engaged optionals.
481 template <typename _Up = _Tp,
483 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
484 __not_<is_same<in_place_t, decay_t<_Up>>>,
485 is_constructible<_Tp, _Up&&>,
486 is_convertible<_Up&&, _Tp>
487 >::value, bool> = true>
488 constexpr optional(_Up&& __t)
489 : _Base(std::in_place, std::forward<_Up>(__t)) { }
491 template <typename _Up = _Tp,
493 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
494 __not_<is_same<in_place_t, decay_t<_Up>>>,
495 is_constructible<_Tp, _Up&&>,
496 __not_<is_convertible<_Up&&, _Tp>>
497 >::value, bool> = false>
498 explicit constexpr optional(_Up&& __t)
499 : _Base(std::in_place, std::forward<_Up>(__t)) { }
501 template <typename _Up,
503 __not_<is_same<_Tp, _Up>>,
504 is_constructible<_Tp, const _Up&>,
505 is_convertible<const _Up&, _Tp>,
506 __not_<__converts_from_optional<_Tp, _Up>>
507 >::value, bool> = true>
508 constexpr optional(const optional<_Up>& __t)
514 template <typename _Up,
516 __not_<is_same<_Tp, _Up>>,
517 is_constructible<_Tp, const _Up&>,
518 __not_<is_convertible<const _Up&, _Tp>>,
519 __not_<__converts_from_optional<_Tp, _Up>>
520 >::value, bool> = false>
521 explicit constexpr optional(const optional<_Up>& __t)
527 template <typename _Up,
529 __not_<is_same<_Tp, _Up>>,
530 is_constructible<_Tp, _Up&&>,
531 is_convertible<_Up&&, _Tp>,
532 __not_<__converts_from_optional<_Tp, _Up>>
533 >::value, bool> = true>
534 constexpr optional(optional<_Up>&& __t)
537 emplace(std::move(*__t));
540 template <typename _Up,
542 __not_<is_same<_Tp, _Up>>,
543 is_constructible<_Tp, _Up&&>,
544 __not_<is_convertible<_Up&&, _Tp>>,
545 __not_<__converts_from_optional<_Tp, _Up>>
546 >::value, bool> = false>
547 explicit constexpr optional(optional<_Up>&& __t)
550 emplace(std::move(*__t));
553 template<typename... _Args,
554 enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
555 explicit constexpr optional(in_place_t, _Args&&... __args)
556 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
558 template<typename _Up, typename... _Args,
559 enable_if_t<is_constructible_v<_Tp,
560 initializer_list<_Up>&,
561 _Args&&...>, bool> = false>
562 explicit constexpr optional(in_place_t,
563 initializer_list<_Up> __il,
565 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
567 // Assignment operators.
569 operator=(nullopt_t) noexcept
575 template<typename _Up = _Tp>
577 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
578 is_constructible<_Tp, _Up>,
579 __not_<__and_<is_scalar<_Tp>,
580 is_same<_Tp, decay_t<_Up>>>>,
581 is_assignable<_Tp&, _Up>>::value,
585 if (this->_M_is_engaged())
586 this->_M_get() = std::forward<_Up>(__u);
588 this->_M_construct(std::forward<_Up>(__u));
593 template<typename _Up>
595 __not_<is_same<_Tp, _Up>>,
596 is_constructible<_Tp, const _Up&>,
597 is_assignable<_Tp&, _Up>,
598 __not_<__converts_from_optional<_Tp, _Up>>,
599 __not_<__assigns_from_optional<_Tp, _Up>>
602 operator=(const optional<_Up>& __u)
606 if (this->_M_is_engaged())
607 this->_M_get() = *__u;
609 this->_M_construct(*__u);
618 template<typename _Up>
620 __not_<is_same<_Tp, _Up>>,
621 is_constructible<_Tp, _Up>,
622 is_assignable<_Tp&, _Up>,
623 __not_<__converts_from_optional<_Tp, _Up>>,
624 __not_<__assigns_from_optional<_Tp, _Up>>
627 operator=(optional<_Up>&& __u)
631 if (this->_M_is_engaged())
632 this->_M_get() = std::move(*__u);
634 this->_M_construct(std::move(*__u));
644 template<typename... _Args>
645 enable_if_t<is_constructible<_Tp, _Args&&...>::value, _Tp&>
646 emplace(_Args&&... __args)
649 this->_M_construct(std::forward<_Args>(__args)...);
650 return this->_M_get();
653 template<typename _Up, typename... _Args>
654 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
655 _Args&&...>::value, _Tp&>
656 emplace(initializer_list<_Up> __il, _Args&&... __args)
659 this->_M_construct(__il, std::forward<_Args>(__args)...);
660 return this->_M_get();
663 // Destructor is implicit, implemented in _Optional_base.
667 swap(optional& __other)
668 noexcept(is_nothrow_move_constructible<_Tp>()
669 && is_nothrow_swappable_v<_Tp>)
673 if (this->_M_is_engaged() && __other._M_is_engaged())
674 swap(this->_M_get(), __other._M_get());
675 else if (this->_M_is_engaged())
677 __other._M_construct(std::move(this->_M_get()));
680 else if (__other._M_is_engaged())
682 this->_M_construct(std::move(__other._M_get()));
683 __other._M_destruct();
690 { return std::__addressof(this->_M_get()); }
694 { return std::__addressof(this->_M_get()); }
698 { return this->_M_get(); }
702 { return this->_M_get(); }
706 { return std::move(this->_M_get()); }
708 constexpr const _Tp&&
710 { return std::move(this->_M_get()); }
712 constexpr explicit operator bool() const noexcept
713 { return this->_M_is_engaged(); }
715 constexpr bool has_value() const noexcept
716 { return this->_M_is_engaged(); }
721 return this->_M_is_engaged()
723 : (__throw_bad_optional_access(),
730 return this->_M_is_engaged()
732 : (__throw_bad_optional_access(),
739 return this->_M_is_engaged()
740 ? std::move(this->_M_get())
741 : (__throw_bad_optional_access(),
742 std::move(this->_M_get()));
745 constexpr const _Tp&&
748 return this->_M_is_engaged()
749 ? std::move(this->_M_get())
750 : (__throw_bad_optional_access(),
751 std::move(this->_M_get()));
754 template<typename _Up>
756 value_or(_Up&& __u) const&
758 static_assert(is_copy_constructible_v<_Tp>);
759 static_assert(is_convertible_v<_Up&&, _Tp>);
761 return this->_M_is_engaged()
763 : static_cast<_Tp>(std::forward<_Up>(__u));
766 template<typename _Up>
768 value_or(_Up&& __u) &&
770 static_assert(is_move_constructible_v<_Tp>);
771 static_assert(is_convertible_v<_Up&&, _Tp>);
773 return this->_M_is_engaged()
774 ? std::move(this->_M_get())
775 : static_cast<_Tp>(std::forward<_Up>(__u));
777 void reset() noexcept { this->_M_reset(); }
780 template<typename _Tp>
781 using __optional_relop_t =
782 enable_if_t<is_convertible<_Tp, bool>::value, bool>;
784 // Comparisons between optional values.
785 template<typename _Tp, typename _Up>
787 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
788 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
790 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
791 && (!__lhs || *__lhs == *__rhs);
794 template<typename _Tp, typename _Up>
796 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
797 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
799 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
800 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
803 template<typename _Tp, typename _Up>
805 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
806 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
808 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
811 template<typename _Tp, typename _Up>
813 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
814 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
816 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
819 template<typename _Tp, typename _Up>
821 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
822 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
824 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
827 template<typename _Tp, typename _Up>
829 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
830 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
832 return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
835 // Comparisons with nullopt.
836 template<typename _Tp>
838 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
841 template<typename _Tp>
843 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
846 template<typename _Tp>
848 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
849 { return static_cast<bool>(__lhs); }
851 template<typename _Tp>
853 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
854 { return static_cast<bool>(__rhs); }
856 template<typename _Tp>
858 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
861 template<typename _Tp>
863 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
864 { return static_cast<bool>(__rhs); }
866 template<typename _Tp>
868 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
869 { return static_cast<bool>(__lhs); }
871 template<typename _Tp>
873 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
876 template<typename _Tp>
878 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
881 template<typename _Tp>
883 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
886 template<typename _Tp>
888 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
891 template<typename _Tp>
893 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
896 // Comparisons with value type.
897 template<typename _Tp, typename _Up>
899 operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
900 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
901 { return __lhs && *__lhs == __rhs; }
903 template<typename _Tp, typename _Up>
905 operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
906 -> __optional_relop_t<decltype(declval<_Up>() == declval<_Tp>())>
907 { return __rhs && __lhs == *__rhs; }
909 template<typename _Tp, typename _Up>
911 operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
912 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
913 { return !__lhs || *__lhs != __rhs; }
915 template<typename _Tp, typename _Up>
917 operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
918 -> __optional_relop_t<decltype(declval<_Up>() != declval<_Tp>())>
919 { return !__rhs || __lhs != *__rhs; }
921 template<typename _Tp, typename _Up>
923 operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
924 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
925 { return !__lhs || *__lhs < __rhs; }
927 template<typename _Tp, typename _Up>
929 operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
930 -> __optional_relop_t<decltype(declval<_Up>() < declval<_Tp>())>
931 { return __rhs && __lhs < *__rhs; }
933 template<typename _Tp, typename _Up>
935 operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
936 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
937 { return __lhs && *__lhs > __rhs; }
939 template<typename _Tp, typename _Up>
941 operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
942 -> __optional_relop_t<decltype(declval<_Up>() > declval<_Tp>())>
943 { return !__rhs || __lhs > *__rhs; }
945 template<typename _Tp, typename _Up>
947 operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
948 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
949 { return !__lhs || *__lhs <= __rhs; }
951 template<typename _Tp, typename _Up>
953 operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
954 -> __optional_relop_t<decltype(declval<_Up>() <= declval<_Tp>())>
955 { return __rhs && __lhs <= *__rhs; }
957 template<typename _Tp, typename _Up>
959 operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
960 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
961 { return __lhs && *__lhs >= __rhs; }
963 template<typename _Tp, typename _Up>
965 operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
966 -> __optional_relop_t<decltype(declval<_Up>() >= declval<_Tp>())>
967 { return !__rhs || __lhs >= *__rhs; }
969 // Swap and creation functions.
971 // _GLIBCXX_RESOLVE_LIB_DEFECTS
972 // 2748. swappable traits for optionals
973 template<typename _Tp>
974 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
975 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
976 noexcept(noexcept(__lhs.swap(__rhs)))
977 { __lhs.swap(__rhs); }
979 template<typename _Tp>
980 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
981 swap(optional<_Tp>&, optional<_Tp>&) = delete;
983 template<typename _Tp>
984 constexpr optional<decay_t<_Tp>>
985 make_optional(_Tp&& __t)
986 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
988 template<typename _Tp, typename ..._Args>
989 constexpr optional<_Tp>
990 make_optional(_Args&&... __args)
991 { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
993 template<typename _Tp, typename _Up, typename ..._Args>
994 constexpr optional<_Tp>
995 make_optional(initializer_list<_Up> __il, _Args&&... __args)
996 { return optional<_Tp> { in_place, __il, std::forward<_Args>(__args)... }; }
1000 template<typename _Tp, bool
1001 = __poison_hash<remove_const_t<_Tp>>::__enable_hash_call>
1002 struct __optional_hash_call_base
1005 operator()(const optional<_Tp>& __t) const
1006 noexcept(noexcept(hash<_Tp> {}(*__t)))
1008 // We pick an arbitrary hash for disengaged optionals which hopefully
1009 // usual values of _Tp won't typically hash to.
1010 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1011 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1015 template<typename _Tp>
1016 struct __optional_hash_call_base<_Tp, false> {};
1018 template<typename _Tp>
1019 struct hash<optional<_Tp>>
1020 : private __poison_hash<remove_const_t<_Tp>>,
1021 public __optional_hash_call_base<_Tp>
1023 using result_type = size_t;
1024 using argument_type = optional<_Tp>;
1029 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1031 _GLIBCXX_END_NAMESPACE_VERSION
1036 #endif // _GLIBCXX_OPTIONAL