1 // <optional> -*- C++ -*-
3 // Copyright (C) 2013-2019 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 experimental/optional
26 * This is a TS C++ Library header.
29 #ifndef _GLIBCXX_EXPERIMENTAL_OPTIONAL
30 #define _GLIBCXX_EXPERIMENTAL_OPTIONAL 1
33 * @defgroup experimental Experimental
35 * Components specified by various Technical Specifications.
37 * As indicated by the std::experimental namespace and the header paths,
38 * the contents of these Technical Specifications are experimental and not
39 * part of the C++ standard. As such the interfaces and implementations may
40 * change in the future, and there is <STRONG> no guarantee of compatibility
41 * between different GCC releases </STRONG> for these features.
44 #if __cplusplus >= 201402L
47 #include <type_traits>
50 #include <initializer_list>
51 #include <bits/functexcept.h>
52 #include <bits/functional_hash.h>
53 #include <bits/enable_special_members.h>
54 #include <experimental/bits/lfts_config.h>
56 namespace std _GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 namespace experimental
62 inline namespace fundamentals_v1
65 * @defgroup optional Optional values
66 * @ingroup experimental
68 * Class template for optional values and surrounding facilities, as
69 * described in n3793 "A proposal to add a utility class to represent
70 * optional objects (Revision 5)".
75 #define __cpp_lib_experimental_optional 201411
77 // All subsequent [X.Y.n] references are against n3793.
80 template<typename _Tp>
84 /// Tag type for in-place construction.
85 struct in_place_t { };
87 /// Tag for in-place construction.
88 constexpr in_place_t in_place { };
91 /// Tag type to disengage optional objects.
94 // Do not user-declare default constructor at all for
95 // optional_value = {} syntax to work.
96 // nullopt_t() = delete;
98 // Used for constructing nullopt.
99 enum class _Construct { _Token };
101 // Must be constexpr for nullopt_t to be literal.
102 explicit constexpr nullopt_t(_Construct) { }
106 /// Tag to disengage optional objects.
107 constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
111 * @brief Exception class thrown when a disengaged optional object is
113 * @ingroup exceptions
115 class bad_optional_access : public logic_error
118 bad_optional_access() : logic_error("bad optional access") { }
120 // XXX This constructor is non-standard. Should not be inline
121 explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
123 virtual ~bad_optional_access() noexcept = default;
127 __throw_bad_optional_access(const char*)
128 __attribute__((__noreturn__));
130 // XXX Does not belong here.
132 __throw_bad_optional_access(const char* __s)
133 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
135 #ifndef __cpp_lib_addressof_constexpr
136 template<typename _Tp, typename = void>
137 struct _Has_addressof_mem : std::false_type { };
139 template<typename _Tp>
140 struct _Has_addressof_mem<_Tp,
141 __void_t<decltype( std::declval<const _Tp&>().operator&() )>
143 : std::true_type { };
145 template<typename _Tp, typename = void>
146 struct _Has_addressof_free : std::false_type { };
148 template<typename _Tp>
149 struct _Has_addressof_free<_Tp,
150 __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
152 : std::true_type { };
155 * @brief Trait that detects the presence of an overloaded unary operator&.
157 * Practically speaking this detects the presence of such an operator when
158 * called on a const-qualified lvalue (e.g.
159 * declval<const _Tp&>().operator&()).
161 template<typename _Tp>
162 struct _Has_addressof
163 : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
167 * @brief An overload that attempts to take the address of an lvalue as a
168 * constant expression. Falls back to __addressof in the presence of an
169 * overloaded addressof operator (unary operator&), in which case the call
170 * will not be a constant expression.
172 template<typename _Tp>
174 enable_if_t<!_Has_addressof<_Tp>::value, _Tp*>
175 __constexpr_addressof(_Tp& __t)
179 * @brief Fallback overload that defers to __addressof.
181 template<typename _Tp>
183 enable_if_t<_Has_addressof<_Tp>::value, _Tp*>
184 __constexpr_addressof(_Tp& __t)
185 { return std::__addressof(__t); }
186 #endif // __cpp_lib_addressof_constexpr
189 * @brief Class template that holds the necessary state for @ref optional
190 * and that has the responsibility for construction and the special members.
192 * Such a separate base class template is necessary in order to
193 * conditionally enable the special members (e.g. copy/move constructors).
194 * Note that this means that @ref _Optional_base implements the
195 * functionality for copy and move assignment, but not for converting
198 * @see optional, _Enable_special_members
200 template<typename _Tp, bool _ShouldProvideDestructor =
201 !is_trivially_destructible<_Tp>::value>
205 // Remove const to avoid prohibition of reusing object storage for
206 // const-qualified types in [3.8/9]. This is strictly internal
207 // and even optional itself is oblivious to it.
208 using _Stored_type = remove_const_t<_Tp>;
211 // [X.Y.4.1] Constructors.
213 // Constructors for disengaged optionals.
214 constexpr _Optional_base() noexcept
217 constexpr _Optional_base(nullopt_t) noexcept
218 : _Optional_base{} { }
220 // Constructors for engaged optionals.
221 template<typename... _Args>
222 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
223 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
225 template<typename _Up, typename... _Args,
226 enable_if_t<is_constructible<_Tp,
227 initializer_list<_Up>&,
230 constexpr explicit _Optional_base(in_place_t,
231 initializer_list<_Up> __il,
233 : _M_payload(__il, std::forward<_Args>(__args)...),
236 // Copy and move constructors.
237 _Optional_base(const _Optional_base& __other)
239 if (__other._M_engaged)
240 this->_M_construct(__other._M_get());
243 _Optional_base(_Optional_base&& __other)
244 noexcept(is_nothrow_move_constructible<_Tp>())
246 if (__other._M_engaged)
247 this->_M_construct(std::move(__other._M_get()));
250 // [X.Y.4.3] (partly) Assignment.
252 operator=(const _Optional_base& __other)
254 if (this->_M_engaged && __other._M_engaged)
255 this->_M_get() = __other._M_get();
258 if (__other._M_engaged)
259 this->_M_construct(__other._M_get());
268 operator=(_Optional_base&& __other)
269 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
270 is_nothrow_move_assignable<_Tp>>())
272 if (this->_M_engaged && __other._M_engaged)
273 this->_M_get() = std::move(__other._M_get());
276 if (__other._M_engaged)
277 this->_M_construct(std::move(__other._M_get()));
284 // [X.Y.4.2] Destructor.
287 if (this->_M_engaged)
288 this->_M_payload.~_Stored_type();
291 // The following functionality is also needed by optional, hence the
292 // protected accessibility.
294 constexpr bool _M_is_engaged() const noexcept
295 { return this->_M_engaged; }
297 // The _M_get operations have _M_engaged as a precondition.
300 { return _M_payload; }
303 _M_get() const noexcept
304 { return _M_payload; }
306 // The _M_construct operation has !_M_engaged as a precondition
307 // while _M_destruct has _M_engaged as a precondition.
308 template<typename... _Args>
310 _M_construct(_Args&&... __args)
311 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
313 ::new (std::__addressof(this->_M_payload))
314 _Stored_type(std::forward<_Args>(__args)...);
315 this->_M_engaged = true;
321 this->_M_engaged = false;
322 this->_M_payload.~_Stored_type();
325 // _M_reset is a 'safe' operation with no precondition.
329 if (this->_M_engaged)
334 struct _Empty_byte { };
336 _Empty_byte _M_empty;
337 _Stored_type _M_payload;
339 bool _M_engaged = false;
342 /// Partial specialization that is exactly identical to the primary template
343 /// save for not providing a destructor, to fulfill triviality requirements.
344 template<typename _Tp>
345 class _Optional_base<_Tp, false>
348 using _Stored_type = remove_const_t<_Tp>;
351 constexpr _Optional_base() noexcept
354 constexpr _Optional_base(nullopt_t) noexcept
355 : _Optional_base{} { }
357 template<typename... _Args>
358 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
359 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
361 template<typename _Up, typename... _Args,
362 enable_if_t<is_constructible<_Tp,
363 initializer_list<_Up>&,
366 constexpr explicit _Optional_base(in_place_t,
367 initializer_list<_Up> __il,
369 : _M_payload(__il, std::forward<_Args>(__args)...),
372 _Optional_base(const _Optional_base& __other)
374 if (__other._M_engaged)
375 this->_M_construct(__other._M_get());
378 _Optional_base(_Optional_base&& __other)
379 noexcept(is_nothrow_move_constructible<_Tp>())
381 if (__other._M_engaged)
382 this->_M_construct(std::move(__other._M_get()));
386 operator=(const _Optional_base& __other)
388 if (this->_M_engaged && __other._M_engaged)
389 this->_M_get() = __other._M_get();
392 if (__other._M_engaged)
393 this->_M_construct(__other._M_get());
401 operator=(_Optional_base&& __other)
402 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
403 is_nothrow_move_assignable<_Tp>>())
405 if (this->_M_engaged && __other._M_engaged)
406 this->_M_get() = std::move(__other._M_get());
409 if (__other._M_engaged)
410 this->_M_construct(std::move(__other._M_get()));
418 // ~_Optional_base() noexcept = default;
421 constexpr bool _M_is_engaged() const noexcept
422 { return this->_M_engaged; }
426 { return _M_payload; }
429 _M_get() const noexcept
430 { return _M_payload; }
432 template<typename... _Args>
434 _M_construct(_Args&&... __args)
435 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
437 ::new (std::__addressof(this->_M_payload))
438 _Stored_type(std::forward<_Args>(__args)...);
439 this->_M_engaged = true;
445 this->_M_engaged = false;
446 this->_M_payload.~_Stored_type();
452 if (this->_M_engaged)
457 struct _Empty_byte { };
460 _Empty_byte _M_empty;
461 _Stored_type _M_payload;
463 bool _M_engaged = false;
466 template<typename _Tp>
469 template<typename _Tp, typename _Up>
470 using __converts_from_optional =
471 __or_<is_constructible<_Tp, const optional<_Up>&>,
472 is_constructible<_Tp, optional<_Up>&>,
473 is_constructible<_Tp, const optional<_Up>&&>,
474 is_constructible<_Tp, optional<_Up>&&>,
475 is_convertible<const optional<_Up>&, _Tp>,
476 is_convertible<optional<_Up>&, _Tp>,
477 is_convertible<const optional<_Up>&&, _Tp>,
478 is_convertible<optional<_Up>&&, _Tp>>;
480 template<typename _Tp, typename _Up>
481 using __assigns_from_optional =
482 __or_<is_assignable<_Tp&, const optional<_Up>&>,
483 is_assignable<_Tp&, optional<_Up>&>,
484 is_assignable<_Tp&, const optional<_Up>&&>,
485 is_assignable<_Tp&, optional<_Up>&&>>;
488 * @brief Class template for optional values.
490 template<typename _Tp>
492 : private _Optional_base<_Tp>,
493 private _Enable_copy_move<
495 is_copy_constructible<_Tp>::value,
497 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
499 is_move_constructible<_Tp>::value,
501 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
505 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
506 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
507 __not_<is_reference<_Tp>>>(),
508 "Invalid instantiation of optional<T>");
511 using _Base = _Optional_base<_Tp>;
514 using value_type = _Tp;
516 // _Optional_base has the responsibility for construction.
519 constexpr optional() = default;
520 // Converting constructors for engaged optionals.
521 template <typename _Up = _Tp,
523 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
524 is_constructible<_Tp, _Up&&>,
525 is_convertible<_Up&&, _Tp>
526 >::value, bool> = true>
527 constexpr optional(_Up&& __t)
528 : _Base(in_place, std::forward<_Up>(__t)) { }
530 template <typename _Up = _Tp,
532 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
533 is_constructible<_Tp, _Up&&>,
534 __not_<is_convertible<_Up&&, _Tp>>
535 >::value, bool> = false>
536 explicit constexpr optional(_Up&& __t)
537 : _Base(in_place, std::forward<_Up>(__t)) { }
539 template <typename _Up,
541 __not_<is_same<_Tp, _Up>>,
542 is_constructible<_Tp, const _Up&>,
543 is_convertible<const _Up&, _Tp>,
544 __not_<__converts_from_optional<_Tp, _Up>>
545 >::value, bool> = true>
546 constexpr optional(const optional<_Up>& __t)
552 template <typename _Up,
554 __not_<is_same<_Tp, _Up>>,
555 is_constructible<_Tp, const _Up&>,
556 __not_<is_convertible<const _Up&, _Tp>>,
557 __not_<__converts_from_optional<_Tp, _Up>>
558 >::value, bool> = false>
559 explicit constexpr optional(const optional<_Up>& __t)
565 template <typename _Up,
567 __not_<is_same<_Tp, _Up>>,
568 is_constructible<_Tp, _Up&&>,
569 is_convertible<_Up&&, _Tp>,
570 __not_<__converts_from_optional<_Tp, _Up>>
571 >::value, bool> = true>
572 constexpr optional(optional<_Up>&& __t)
575 emplace(std::move(*__t));
578 template <typename _Up,
580 __not_<is_same<_Tp, _Up>>,
581 is_constructible<_Tp, _Up&&>,
582 __not_<is_convertible<_Up&&, _Tp>>,
583 __not_<__converts_from_optional<_Tp, _Up>>
584 >::value, bool> = false>
585 explicit constexpr optional(optional<_Up>&& __t)
588 emplace(std::move(*__t));
591 // [X.Y.4.3] (partly) Assignment.
593 operator=(nullopt_t) noexcept
599 template<typename _Up = _Tp>
601 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
602 is_constructible<_Tp, _Up>,
603 __not_<__and_<is_scalar<_Tp>,
604 is_same<_Tp, decay_t<_Up>>>>,
605 is_assignable<_Tp&, _Up>>::value,
609 if (this->_M_is_engaged())
610 this->_M_get() = std::forward<_Up>(__u);
612 this->_M_construct(std::forward<_Up>(__u));
617 template<typename _Up>
619 __not_<is_same<_Tp, _Up>>,
620 is_constructible<_Tp, const _Up&>,
621 is_assignable<_Tp&, _Up>,
622 __not_<__converts_from_optional<_Tp, _Up>>,
623 __not_<__assigns_from_optional<_Tp, _Up>>
626 operator=(const optional<_Up>& __u)
630 if (this->_M_is_engaged())
631 this->_M_get() = *__u;
633 this->_M_construct(*__u);
642 template<typename _Up>
644 __not_<is_same<_Tp, _Up>>,
645 is_constructible<_Tp, _Up>,
646 is_assignable<_Tp&, _Up>,
647 __not_<__converts_from_optional<_Tp, _Up>>,
648 __not_<__assigns_from_optional<_Tp, _Up>>
651 operator=(optional<_Up>&& __u)
655 if (this->_M_is_engaged())
656 this->_M_get() = std::move(*__u);
658 this->_M_construct(std::move(*__u));
668 template<typename... _Args>
669 enable_if_t<is_constructible<_Tp, _Args&&...>::value>
670 emplace(_Args&&... __args)
673 this->_M_construct(std::forward<_Args>(__args)...);
676 template<typename _Up, typename... _Args>
677 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
679 emplace(initializer_list<_Up> __il, _Args&&... __args)
682 this->_M_construct(__il, std::forward<_Args>(__args)...);
685 // [X.Y.4.2] Destructor is implicit, implemented in _Optional_base.
689 swap(optional& __other)
690 noexcept(is_nothrow_move_constructible<_Tp>()
691 && __is_nothrow_swappable<_Tp>::value)
695 if (this->_M_is_engaged() && __other._M_is_engaged())
696 swap(this->_M_get(), __other._M_get());
697 else if (this->_M_is_engaged())
699 __other._M_construct(std::move(this->_M_get()));
702 else if (__other._M_is_engaged())
704 this->_M_construct(std::move(__other._M_get()));
705 __other._M_destruct();
709 // [X.Y.4.5] Observers.
713 #ifndef __cpp_lib_addressof_constexpr
714 return __constexpr_addressof(this->_M_get());
716 return std::__addressof(this->_M_get());
722 { return std::__addressof(this->_M_get()); }
726 { return this->_M_get(); }
730 { return this->_M_get(); }
734 { return std::move(this->_M_get()); }
736 constexpr const _Tp&&
738 { return std::move(this->_M_get()); }
740 constexpr explicit operator bool() const noexcept
741 { return this->_M_is_engaged(); }
746 return this->_M_is_engaged()
748 : (__throw_bad_optional_access("Attempt to access value of a "
749 "disengaged optional object"),
756 return this->_M_is_engaged()
758 : (__throw_bad_optional_access("Attempt to access value of a "
759 "disengaged optional object"),
766 return this->_M_is_engaged()
767 ? std::move(this->_M_get())
768 : (__throw_bad_optional_access("Attempt to access value of a "
769 "disengaged optional object"),
770 std::move(this->_M_get()));
773 constexpr const _Tp&&
776 return this->_M_is_engaged()
777 ? std::move(this->_M_get())
778 : (__throw_bad_optional_access("Attempt to access value of a "
779 "disengaged optional object"),
780 std::move(this->_M_get()));
783 template<typename _Up>
785 value_or(_Up&& __u) const&
787 static_assert(__and_<is_copy_constructible<_Tp>,
788 is_convertible<_Up&&, _Tp>>(),
789 "Cannot return value");
791 return this->_M_is_engaged()
793 : static_cast<_Tp>(std::forward<_Up>(__u));
796 template<typename _Up>
798 value_or(_Up&& __u) &&
800 static_assert(__and_<is_move_constructible<_Tp>,
801 is_convertible<_Up&&, _Tp>>(),
802 "Cannot return value" );
804 return this->_M_is_engaged()
805 ? std::move(this->_M_get())
806 : static_cast<_Tp>(std::forward<_Up>(__u));
810 // [X.Y.8] Comparisons between optional values.
811 template<typename _Tp>
813 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
815 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
816 && (!__lhs || *__lhs == *__rhs);
819 template<typename _Tp>
821 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
822 { return !(__lhs == __rhs); }
824 template<typename _Tp>
826 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
828 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
831 template<typename _Tp>
833 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
834 { return __rhs < __lhs; }
836 template<typename _Tp>
838 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
839 { return !(__rhs < __lhs); }
841 template<typename _Tp>
843 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
844 { return !(__lhs < __rhs); }
846 // [X.Y.9] Comparisons with nullopt.
847 template<typename _Tp>
849 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
852 template<typename _Tp>
854 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
857 template<typename _Tp>
859 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
860 { return static_cast<bool>(__lhs); }
862 template<typename _Tp>
864 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
865 { return static_cast<bool>(__rhs); }
867 template<typename _Tp>
869 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
872 template<typename _Tp>
874 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
875 { return static_cast<bool>(__rhs); }
877 template<typename _Tp>
879 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
880 { return static_cast<bool>(__lhs); }
882 template<typename _Tp>
884 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
887 template<typename _Tp>
889 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
892 template<typename _Tp>
894 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
897 template<typename _Tp>
899 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
902 template<typename _Tp>
904 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
907 // [X.Y.10] Comparisons with value type.
908 template<typename _Tp>
910 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
911 { return __lhs && *__lhs == __rhs; }
913 template<typename _Tp>
915 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
916 { return __rhs && __lhs == *__rhs; }
918 template<typename _Tp>
920 operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
921 { return !__lhs || !(*__lhs == __rhs); }
923 template<typename _Tp>
925 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
926 { return !__rhs || !(__lhs == *__rhs); }
928 template<typename _Tp>
930 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
931 { return !__lhs || *__lhs < __rhs; }
933 template<typename _Tp>
935 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
936 { return __rhs && __lhs < *__rhs; }
938 template<typename _Tp>
940 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
941 { return __lhs && __rhs < *__lhs; }
943 template<typename _Tp>
945 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
946 { return !__rhs || *__rhs < __lhs; }
948 template<typename _Tp>
950 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
951 { return !__lhs || !(__rhs < *__lhs); }
953 template<typename _Tp>
955 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
956 { return __rhs && !(*__rhs < __lhs); }
958 template<typename _Tp>
960 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
961 { return __lhs && !(*__lhs < __rhs); }
963 template<typename _Tp>
965 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
966 { return !__rhs || !(__lhs < *__rhs); }
969 template<typename _Tp>
971 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
972 noexcept(noexcept(__lhs.swap(__rhs)))
973 { __lhs.swap(__rhs); }
975 template<typename _Tp>
976 constexpr optional<decay_t<_Tp>>
977 make_optional(_Tp&& __t)
978 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
981 } // namespace fundamentals_v1
982 } // namespace experimental
985 template<typename _Tp>
986 struct hash<experimental::optional<_Tp>>
988 using result_type = size_t;
989 using argument_type = experimental::optional<_Tp>;
992 operator()(const experimental::optional<_Tp>& __t) const
993 noexcept(noexcept(hash<_Tp> {}(*__t)))
995 // We pick an arbitrary hash for disengaged optionals which hopefully
996 // usual values of _Tp won't typically hash to.
997 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
998 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1002 _GLIBCXX_END_NAMESPACE_VERSION
1007 #endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL