1 // <optional> -*- C++ -*-
3 // Copyright (C) 2013-2016 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 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 logic_error
81 // XXX See LEWG 72, https://issues.isocpp.org/show_bug.cgi?id=72
83 bad_optional_access() : logic_error("bad optional access") { }
84 // XXX This constructor is non-standard. Should not be inline
85 explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
87 virtual ~bad_optional_access() noexcept = default;
91 __throw_bad_optional_access(const char*)
92 __attribute__((__noreturn__));
94 // XXX Does not belong here.
96 __throw_bad_optional_access(const char* __s)
97 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
99 template<typename _Tp, typename = void>
100 struct _Has_addressof_mem : std::false_type { };
102 template<typename _Tp>
103 struct _Has_addressof_mem<_Tp,
104 __void_t<decltype( std::declval<const _Tp&>().operator&() )>
106 : std::true_type { };
108 template<typename _Tp, typename = void>
109 struct _Has_addressof_free : std::false_type { };
111 template<typename _Tp>
112 struct _Has_addressof_free<_Tp,
113 __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
115 : std::true_type { };
118 * @brief Trait that detects the presence of an overloaded unary operator&.
120 * Practically speaking this detects the presence of such an operator when
121 * called on a const-qualified lvalue (e.g.
122 * declval<const _Tp&>().operator&()).
124 template<typename _Tp>
125 struct _Has_addressof
126 : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
130 * @brief An overload that attempts to take the address of an lvalue as a
131 * constant expression. Falls back to __addressof in the presence of an
132 * overloaded addressof operator (unary operator&), in which case the call
133 * will not be a constant expression.
135 template<typename _Tp, enable_if_t<!_Has_addressof<_Tp>::value, int>...>
136 constexpr _Tp* __constexpr_addressof(_Tp& __t)
140 * @brief Fallback overload that defers to __addressof.
142 template<typename _Tp, enable_if_t<_Has_addressof<_Tp>::value, int>...>
143 inline _Tp* __constexpr_addressof(_Tp& __t)
144 { return std::__addressof(__t); }
147 * @brief Class template that holds the necessary state for @ref optional
148 * and that has the responsibility for construction and the special members.
150 * Such a separate base class template is necessary in order to
151 * conditionally enable the special members (e.g. copy/move constructors).
152 * Note that this means that @ref _Optional_base implements the
153 * functionality for copy and move assignment, but not for converting
156 * @see optional, _Enable_special_members
158 template<typename _Tp, bool _ShouldProvideDestructor =
159 !is_trivially_destructible<_Tp>::value>
163 // Remove const to avoid prohibition of reusing object storage for
164 // const-qualified types in [3.8/9]. This is strictly internal
165 // and even optional itself is oblivious to it.
166 using _Stored_type = remove_const_t<_Tp>;
170 // Constructors for disengaged optionals.
171 constexpr _Optional_base() noexcept
174 constexpr _Optional_base(nullopt_t) noexcept
175 : _Optional_base{} { }
177 // Constructors for engaged optionals.
178 template<typename... _Args>
179 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
180 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
182 template<typename _Up, typename... _Args,
183 enable_if_t<is_constructible<_Tp,
184 initializer_list<_Up>&,
187 constexpr explicit _Optional_base(in_place_t,
188 initializer_list<_Up> __il,
190 : _M_payload(__il, std::forward<_Args>(__args)...),
193 // Copy and move constructors.
194 _Optional_base(const _Optional_base& __other)
196 if (__other._M_engaged)
197 this->_M_construct(__other._M_get());
200 _Optional_base(_Optional_base&& __other)
201 noexcept(is_nothrow_move_constructible<_Tp>())
203 if (__other._M_engaged)
204 this->_M_construct(std::move(__other._M_get()));
207 // Assignment operators.
209 operator=(const _Optional_base& __other)
211 if (this->_M_engaged && __other._M_engaged)
212 this->_M_get() = __other._M_get();
215 if (__other._M_engaged)
216 this->_M_construct(__other._M_get());
225 operator=(_Optional_base&& __other)
226 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
227 is_nothrow_move_assignable<_Tp>>())
229 if (this->_M_engaged && __other._M_engaged)
230 this->_M_get() = std::move(__other._M_get());
233 if (__other._M_engaged)
234 this->_M_construct(std::move(__other._M_get()));
244 if (this->_M_engaged)
245 this->_M_payload.~_Stored_type();
248 // The following functionality is also needed by optional, hence the
249 // protected accessibility.
251 constexpr bool _M_is_engaged() const noexcept
252 { return this->_M_engaged; }
254 // The _M_get operations have _M_engaged as a precondition.
257 { return _M_payload; }
260 _M_get() const noexcept
261 { return _M_payload; }
263 // The _M_construct operation has !_M_engaged as a precondition
264 // while _M_destruct has _M_engaged as a precondition.
265 template<typename... _Args>
267 _M_construct(_Args&&... __args)
268 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
270 ::new (std::__addressof(this->_M_payload))
271 _Stored_type(std::forward<_Args>(__args)...);
272 this->_M_engaged = true;
278 this->_M_engaged = false;
279 this->_M_payload.~_Stored_type();
282 // _M_reset is a 'safe' operation with no precondition.
286 if (this->_M_engaged)
291 struct _Empty_byte { };
293 _Empty_byte _M_empty;
294 _Stored_type _M_payload;
296 bool _M_engaged = false;
299 /// Partial specialization that is exactly identical to the primary template
300 /// save for not providing a destructor, to fulfill triviality requirements.
301 template<typename _Tp>
302 class _Optional_base<_Tp, false>
305 using _Stored_type = remove_const_t<_Tp>;
308 constexpr _Optional_base() noexcept
311 constexpr _Optional_base(nullopt_t) noexcept
312 : _Optional_base{} { }
314 template<typename... _Args>
315 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
316 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
318 template<typename _Up, typename... _Args,
319 enable_if_t<is_constructible<_Tp,
320 initializer_list<_Up>&,
323 constexpr explicit _Optional_base(in_place_t,
324 initializer_list<_Up> __il,
326 : _M_payload(__il, std::forward<_Args>(__args)...),
329 _Optional_base(const _Optional_base& __other)
331 if (__other._M_engaged)
332 this->_M_construct(__other._M_get());
335 _Optional_base(_Optional_base&& __other)
336 noexcept(is_nothrow_move_constructible<_Tp>())
338 if (__other._M_engaged)
339 this->_M_construct(std::move(__other._M_get()));
343 operator=(const _Optional_base& __other)
345 if (this->_M_engaged && __other._M_engaged)
346 this->_M_get() = __other._M_get();
349 if (__other._M_engaged)
350 this->_M_construct(__other._M_get());
358 operator=(_Optional_base&& __other)
359 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
360 is_nothrow_move_assignable<_Tp>>())
362 if (this->_M_engaged && __other._M_engaged)
363 this->_M_get() = std::move(__other._M_get());
366 if (__other._M_engaged)
367 this->_M_construct(std::move(__other._M_get()));
375 // ~_Optional_base() noexcept = default;
378 constexpr bool _M_is_engaged() const noexcept
379 { return this->_M_engaged; }
383 { return _M_payload; }
386 _M_get() const noexcept
387 { return _M_payload; }
389 template<typename... _Args>
391 _M_construct(_Args&&... __args)
392 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
394 ::new (std::__addressof(this->_M_payload))
395 _Stored_type(std::forward<_Args>(__args)...);
396 this->_M_engaged = true;
402 this->_M_engaged = false;
403 this->_M_payload.~_Stored_type();
409 if (this->_M_engaged)
414 struct _Empty_byte { };
417 _Empty_byte _M_empty;
418 _Stored_type _M_payload;
420 bool _M_engaged = false;
423 template<typename _Tp>
426 template<typename _Tp, typename _Up>
427 using __converts_from_optional =
428 __or_<is_constructible<_Tp, const optional<_Up>&>,
429 is_constructible<_Tp, optional<_Up>&>,
430 is_constructible<_Tp, const optional<_Up>&&>,
431 is_constructible<_Tp, optional<_Up>&&>,
432 is_convertible<const optional<_Up>&, _Tp>,
433 is_convertible<optional<_Up>&, _Tp>,
434 is_convertible<const optional<_Up>&&, _Tp>,
435 is_convertible<optional<_Up>&&, _Tp>>;
437 template<typename _Tp, typename _Up>
438 using __assigns_from_optional =
439 __or_<is_assignable<_Tp&, const optional<_Up>&>,
440 is_assignable<_Tp&, optional<_Up>&>,
441 is_assignable<_Tp&, const optional<_Up>&&>,
442 is_assignable<_Tp&, optional<_Up>&&>>;
445 * @brief Class template for optional values.
447 template<typename _Tp>
449 : private _Optional_base<_Tp>,
450 private _Enable_copy_move<
452 is_copy_constructible<_Tp>::value,
454 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
456 is_move_constructible<_Tp>::value,
458 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
462 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
463 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
464 __not_<is_reference<_Tp>>>(),
465 "Invalid instantiation of optional<T>");
468 using _Base = _Optional_base<_Tp>;
471 using value_type = _Tp;
473 constexpr optional() = default;
475 constexpr optional(nullopt_t) noexcept
478 // Converting constructors for engaged optionals.
479 template <typename _Up = _Tp,
481 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
482 is_constructible<_Tp, _Up&&>,
483 is_convertible<_Up&&, _Tp>
484 >::value, bool> = true>
485 constexpr optional(_Up&& __t)
486 : _Base(std::in_place, std::forward<_Up>(__t)) { }
488 template <typename _Up = _Tp,
490 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
491 is_constructible<_Tp, _Up&&>,
492 __not_<is_convertible<_Up&&, _Tp>>
493 >::value, bool> = false>
494 explicit constexpr optional(_Up&& __t)
495 : _Base(std::in_place, std::forward<_Up>(__t)) { }
497 template <typename _Up,
499 __not_<is_same<_Tp, _Up>>,
500 is_constructible<_Tp, const _Up&>,
501 is_convertible<const _Up&, _Tp>,
502 __not_<__converts_from_optional<_Tp, _Up>>
503 >::value, bool> = true>
504 constexpr optional(const optional<_Up>& __t)
510 template <typename _Up,
512 __not_<is_same<_Tp, _Up>>,
513 is_constructible<_Tp, const _Up&>,
514 __not_<is_convertible<const _Up&, _Tp>>,
515 __not_<__converts_from_optional<_Tp, _Up>>
516 >::value, bool> = false>
517 explicit constexpr optional(const optional<_Up>& __t)
523 template <typename _Up,
525 __not_<is_same<_Tp, _Up>>,
526 is_constructible<_Tp, _Up&&>,
527 is_convertible<_Up&&, _Tp>,
528 __not_<__converts_from_optional<_Tp, _Up>>
529 >::value, bool> = true>
530 constexpr optional(optional<_Up>&& __t)
533 emplace(std::move(*__t));
536 template <typename _Up,
538 __not_<is_same<_Tp, _Up>>,
539 is_constructible<_Tp, _Up&&>,
540 __not_<is_convertible<_Up&&, _Tp>>,
541 __not_<__converts_from_optional<_Tp, _Up>>
542 >::value, bool> = false>
543 explicit constexpr optional(optional<_Up>&& __t)
546 emplace(std::move(*__t));
549 template<typename... _Args>
550 explicit constexpr optional(in_place_t, _Args&&... __args)
551 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
553 template<typename _Up, typename... _Args,
554 enable_if_t<is_constructible<_Tp,
555 initializer_list<_Up>&,
558 explicit constexpr optional(in_place_t,
559 initializer_list<_Up> __il,
561 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
563 // Assignment operators.
565 operator=(nullopt_t) noexcept
571 template<typename _Up = _Tp,
573 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
574 is_constructible<_Tp, _Up>,
575 __not_<__and_<is_scalar<_Tp>,
576 is_same<_Tp, decay_t<_Up>>>>,
577 is_assignable<_Tp&, _Up>>::value,
582 if (this->_M_is_engaged())
583 this->_M_get() = std::forward<_Up>(__u);
585 this->_M_construct(std::forward<_Up>(__u));
590 template<typename _Up,
592 __not_<is_same<_Tp, _Up>>,
593 is_constructible<_Tp, const _Up&>,
594 is_assignable<_Tp&, _Up>,
595 __not_<__converts_from_optional<_Tp, _Up>>,
596 __not_<__assigns_from_optional<_Tp, _Up>>
600 operator=(const optional<_Up>& __u)
604 if (this->_M_is_engaged())
605 this->_M_get() = *__u;
607 this->_M_construct(*__u);
616 template<typename _Up,
618 __not_<is_same<_Tp, _Up>>,
619 is_constructible<_Tp, _Up>,
620 is_assignable<_Tp&, _Up>,
621 __not_<__converts_from_optional<_Tp, _Up>>,
622 __not_<__assigns_from_optional<_Tp, _Up>>
626 operator=(optional<_Up>&& __u)
630 if (this->_M_is_engaged())
631 this->_M_get() = std::move(*__u);
633 this->_M_construct(std::move(*__u));
643 template<typename... _Args>
644 enable_if_t<is_constructible<_Tp, _Args&&...>::value>
645 emplace(_Args&&... __args)
648 this->_M_construct(std::forward<_Args>(__args)...);
651 template<typename _Up, typename... _Args>
652 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
654 emplace(initializer_list<_Up> __il, _Args&&... __args)
657 this->_M_construct(__il, std::forward<_Args>(__args)...);
660 // Destructor is implicit, implemented in _Optional_base.
664 swap(optional& __other)
665 noexcept(is_nothrow_move_constructible<_Tp>()
666 && noexcept(swap(declval<_Tp&>(), declval<_Tp&>())))
670 if (this->_M_is_engaged() && __other._M_is_engaged())
671 swap(this->_M_get(), __other._M_get());
672 else if (this->_M_is_engaged())
674 __other._M_construct(std::move(this->_M_get()));
677 else if (__other._M_is_engaged())
679 this->_M_construct(std::move(__other._M_get()));
680 __other._M_destruct();
687 { return __constexpr_addressof(this->_M_get()); }
691 { return std::__addressof(this->_M_get()); }
695 { return this->_M_get(); }
699 { return this->_M_get(); }
703 { return std::move(this->_M_get()); }
705 constexpr const _Tp&&
707 { return std::move(this->_M_get()); }
709 constexpr explicit operator bool() const noexcept
710 { return this->_M_is_engaged(); }
712 constexpr bool has_value() const noexcept
713 { return this->_M_is_engaged(); }
718 return this->_M_is_engaged()
720 : (__throw_bad_optional_access("Attempt to access value of a "
721 "disengaged optional object"),
728 return this->_M_is_engaged()
730 : (__throw_bad_optional_access("Attempt to access value of a "
731 "disengaged optional object"),
738 return this->_M_is_engaged()
739 ? std::move(this->_M_get())
740 : (__throw_bad_optional_access("Attempt to access value of a "
741 "disengaged optional object"),
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("Attempt to access value of a "
751 "disengaged optional object"),
752 std::move(this->_M_get()));
755 template<typename _Up>
757 value_or(_Up&& __u) const&
759 static_assert(__and_<is_copy_constructible<_Tp>,
760 is_convertible<_Up&&, _Tp>>(),
761 "Cannot return value");
763 return this->_M_is_engaged()
765 : static_cast<_Tp>(std::forward<_Up>(__u));
768 template<typename _Up>
770 value_or(_Up&& __u) &&
772 static_assert(__and_<is_move_constructible<_Tp>,
773 is_convertible<_Up&&, _Tp>>(),
774 "Cannot return value" );
776 return this->_M_is_engaged()
777 ? std::move(this->_M_get())
778 : static_cast<_Tp>(std::forward<_Up>(__u));
780 void reset() noexcept { this->_M_reset(); }
783 template<typename _Tp>
784 using __optional_relop_t =
785 enable_if_t<is_convertible<_Tp, bool>::value, bool>;
787 // Comparisons between optional values.
788 template<typename _Tp>
790 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
791 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
793 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
794 && (!__lhs || *__lhs == *__rhs);
797 template<typename _Tp>
799 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
800 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
802 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
803 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
806 template<typename _Tp>
808 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
809 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
811 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
814 template<typename _Tp>
816 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
817 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
819 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
822 template<typename _Tp>
824 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
825 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
827 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
830 template<typename _Tp>
832 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
833 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
835 return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
838 // Comparisons with nullopt.
839 template<typename _Tp>
841 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
844 template<typename _Tp>
846 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
849 template<typename _Tp>
851 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
852 { return static_cast<bool>(__lhs); }
854 template<typename _Tp>
856 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
857 { return static_cast<bool>(__rhs); }
859 template<typename _Tp>
861 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
864 template<typename _Tp>
866 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
867 { return static_cast<bool>(__rhs); }
869 template<typename _Tp>
871 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
872 { return static_cast<bool>(__lhs); }
874 template<typename _Tp>
876 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
879 template<typename _Tp>
881 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
884 template<typename _Tp>
886 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
889 template<typename _Tp>
891 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
894 template<typename _Tp>
896 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
899 // Comparisons with value type.
900 template<typename _Tp>
902 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
903 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
904 { return __lhs && *__lhs == __rhs; }
906 template<typename _Tp>
908 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
909 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
910 { return __rhs && __lhs == *__rhs; }
912 template<typename _Tp>
914 operator!=(const optional<_Tp>& __lhs, const _Tp& __rhs)
915 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
916 { return !__lhs || *__lhs != __rhs; }
918 template<typename _Tp>
920 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
921 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
922 { return !__rhs || __lhs != *__rhs; }
924 template<typename _Tp>
926 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
927 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
928 { return !__lhs || *__lhs < __rhs; }
930 template<typename _Tp>
932 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
933 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
934 { return __rhs && __lhs < *__rhs; }
936 template<typename _Tp>
938 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
939 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
940 { return __lhs && *__lhs > __rhs; }
942 template<typename _Tp>
944 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
945 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
946 { return !__rhs || __lhs > *__rhs; }
948 template<typename _Tp>
950 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
951 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
952 { return !__lhs || *__lhs <= __rhs; }
954 template<typename _Tp>
956 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
957 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
958 { return __rhs && __lhs <= *__rhs; }
960 template<typename _Tp>
962 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
963 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
964 { return __lhs && *__lhs >= __rhs; }
966 template<typename _Tp>
968 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
969 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
970 { return !__rhs || __lhs >= *__rhs; }
972 // Swap and creation functions.
973 template<typename _Tp>
975 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
976 noexcept(noexcept(__lhs.swap(__rhs)))
977 { __lhs.swap(__rhs); }
979 template<typename _Tp>
980 constexpr optional<decay_t<_Tp>>
981 make_optional(_Tp&& __t)
982 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
984 template<typename _Tp, typename ..._Args>
985 constexpr optional<_Tp>
986 make_optional(_Args&&... __args)
987 { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
989 template<typename _Tp, typename _Up, typename ..._Args>
990 constexpr optional<_Tp>
991 make_optional(initializer_list<_Up> __il, _Args&&... __args)
992 { return optional<_Tp> { in_place, __il, std::forward<_Args>(__args)... }; }
995 template<typename _Tp>
996 struct hash<optional<_Tp>>
998 using result_type = size_t;
999 using argument_type = optional<_Tp>;
1002 operator()(const optional<_Tp>& __t) const
1003 noexcept(noexcept(hash<_Tp> {}(*__t)))
1005 // We pick an arbitrary hash for disengaged optionals which hopefully
1006 // usual values of _Tp won't typically hash to.
1007 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1008 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1014 _GLIBCXX_END_NAMESPACE_VERSION
1019 #endif // _GLIBCXX_OPTIONAL