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,
581 if (this->_M_is_engaged())
582 this->_M_get() = std::forward<_Up>(__u);
584 this->_M_construct(std::forward<_Up>(__u));
589 template<typename _Up>
591 __not_<is_same<_Tp, _Up>>,
592 is_constructible<_Tp, const _Up&>,
593 is_assignable<_Tp&, _Up>,
594 __not_<__converts_from_optional<_Tp, _Up>>,
595 __not_<__assigns_from_optional<_Tp, _Up>>
598 operator=(const optional<_Up>& __u)
602 if (this->_M_is_engaged())
603 this->_M_get() = *__u;
605 this->_M_construct(*__u);
614 template<typename _Up>
616 __not_<is_same<_Tp, _Up>>,
617 is_constructible<_Tp, _Up>,
618 is_assignable<_Tp&, _Up>,
619 __not_<__converts_from_optional<_Tp, _Up>>,
620 __not_<__assigns_from_optional<_Tp, _Up>>
623 operator=(optional<_Up>&& __u)
627 if (this->_M_is_engaged())
628 this->_M_get() = std::move(*__u);
630 this->_M_construct(std::move(*__u));
640 template<typename... _Args>
641 enable_if_t<is_constructible<_Tp, _Args&&...>::value>
642 emplace(_Args&&... __args)
645 this->_M_construct(std::forward<_Args>(__args)...);
648 template<typename _Up, typename... _Args>
649 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
651 emplace(initializer_list<_Up> __il, _Args&&... __args)
654 this->_M_construct(__il, std::forward<_Args>(__args)...);
657 // Destructor is implicit, implemented in _Optional_base.
661 swap(optional& __other)
662 noexcept(is_nothrow_move_constructible<_Tp>()
663 && noexcept(swap(declval<_Tp&>(), declval<_Tp&>())))
667 if (this->_M_is_engaged() && __other._M_is_engaged())
668 swap(this->_M_get(), __other._M_get());
669 else if (this->_M_is_engaged())
671 __other._M_construct(std::move(this->_M_get()));
674 else if (__other._M_is_engaged())
676 this->_M_construct(std::move(__other._M_get()));
677 __other._M_destruct();
684 { return __constexpr_addressof(this->_M_get()); }
688 { return std::__addressof(this->_M_get()); }
692 { return this->_M_get(); }
696 { return this->_M_get(); }
700 { return std::move(this->_M_get()); }
702 constexpr const _Tp&&
704 { return std::move(this->_M_get()); }
706 constexpr explicit operator bool() const noexcept
707 { return this->_M_is_engaged(); }
709 constexpr bool has_value() const noexcept
710 { return this->_M_is_engaged(); }
715 return this->_M_is_engaged()
717 : (__throw_bad_optional_access("Attempt to access value of a "
718 "disengaged optional object"),
725 return this->_M_is_engaged()
727 : (__throw_bad_optional_access("Attempt to access value of a "
728 "disengaged optional object"),
735 return this->_M_is_engaged()
736 ? std::move(this->_M_get())
737 : (__throw_bad_optional_access("Attempt to access value of a "
738 "disengaged optional object"),
739 std::move(this->_M_get()));
742 constexpr const _Tp&&
745 return this->_M_is_engaged()
746 ? std::move(this->_M_get())
747 : (__throw_bad_optional_access("Attempt to access value of a "
748 "disengaged optional object"),
749 std::move(this->_M_get()));
752 template<typename _Up>
754 value_or(_Up&& __u) const&
756 static_assert(__and_<is_copy_constructible<_Tp>,
757 is_convertible<_Up&&, _Tp>>(),
758 "Cannot return value");
760 return this->_M_is_engaged()
762 : static_cast<_Tp>(std::forward<_Up>(__u));
765 template<typename _Up>
767 value_or(_Up&& __u) &&
769 static_assert(__and_<is_move_constructible<_Tp>,
770 is_convertible<_Up&&, _Tp>>(),
771 "Cannot return value" );
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>
787 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
788 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
790 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
791 && (!__lhs || *__lhs == *__rhs);
794 template<typename _Tp>
796 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
797 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
799 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
800 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
803 template<typename _Tp>
805 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
806 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
808 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
811 template<typename _Tp>
813 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
814 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
816 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
819 template<typename _Tp>
821 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
822 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
824 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
827 template<typename _Tp>
829 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
830 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
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>
899 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
900 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
901 { return __lhs && *__lhs == __rhs; }
903 template<typename _Tp>
905 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
906 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
907 { return __rhs && __lhs == *__rhs; }
909 template<typename _Tp>
911 operator!=(const optional<_Tp>& __lhs, const _Tp& __rhs)
912 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
913 { return !__lhs || *__lhs != __rhs; }
915 template<typename _Tp>
917 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
918 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
919 { return !__rhs || __lhs != *__rhs; }
921 template<typename _Tp>
923 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
924 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
925 { return !__lhs || *__lhs < __rhs; }
927 template<typename _Tp>
929 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
930 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
931 { return __rhs && __lhs < *__rhs; }
933 template<typename _Tp>
935 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
936 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
937 { return __lhs && *__lhs > __rhs; }
939 template<typename _Tp>
941 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
942 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
943 { return !__rhs || __lhs > *__rhs; }
945 template<typename _Tp>
947 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
948 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
949 { return !__lhs || *__lhs <= __rhs; }
951 template<typename _Tp>
953 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
954 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
955 { return __rhs && __lhs <= *__rhs; }
957 template<typename _Tp>
959 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
960 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
961 { return __lhs && *__lhs >= __rhs; }
963 template<typename _Tp>
965 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
966 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
967 { return !__rhs || __lhs >= *__rhs; }
969 // Swap and creation functions.
970 template<typename _Tp>
972 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
973 noexcept(noexcept(__lhs.swap(__rhs)))
974 { __lhs.swap(__rhs); }
976 template<typename _Tp>
977 constexpr optional<decay_t<_Tp>>
978 make_optional(_Tp&& __t)
979 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
981 template<typename _Tp, typename ..._Args>
982 constexpr optional<_Tp>
983 make_optional(_Args&&... __args)
984 { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
986 template<typename _Tp, typename _Up, typename ..._Args>
987 constexpr optional<_Tp>
988 make_optional(initializer_list<_Up> __il, _Args&&... __args)
989 { return optional<_Tp> { in_place, __il, std::forward<_Args>(__args)... }; }
992 template<typename _Tp>
993 struct hash<optional<_Tp>>
995 using result_type = size_t;
996 using argument_type = optional<_Tp>;
999 operator()(const optional<_Tp>& __t) const
1000 noexcept(noexcept(hash<_Tp> {}(*__t)))
1002 // We pick an arbitrary hash for disengaged optionals which hopefully
1003 // usual values of _Tp won't typically hash to.
1004 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1005 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1011 _GLIBCXX_END_NAMESPACE_VERSION
1016 #endif // _GLIBCXX_OPTIONAL