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 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 <= 201103L
45 # include <bits/c++14_warning.h>
49 #include <type_traits>
52 #include <initializer_list>
53 #include <bits/functexcept.h>
54 #include <bits/functional_hash.h>
55 #include <bits/enable_special_members.h>
56 #include <experimental/bits/lfts_config.h>
58 namespace std _GLIBCXX_VISIBILITY(default)
60 namespace experimental
62 inline namespace fundamentals_v1
64 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 * @defgroup optional Optional values
68 * @ingroup experimental
70 * Class template for optional values and surrounding facilities, as
71 * described in n3793 "A proposal to add a utility class to represent
72 * optional objects (Revision 5)".
77 #define __cpp_lib_experimental_optional 201411
79 // All subsequent [X.Y.n] references are against n3793.
82 template<typename _Tp>
86 /// Tag type for in-place construction.
87 struct in_place_t { };
89 /// Tag for in-place construction.
90 constexpr in_place_t in_place { };
93 /// Tag type to disengage optional objects.
96 // Do not user-declare default constructor at all for
97 // optional_value = {} syntax to work.
98 // nullopt_t() = delete;
100 // Used for constructing nullopt.
101 enum class _Construct { _Token };
103 // Must be constexpr for nullopt_t to be literal.
104 explicit constexpr nullopt_t(_Construct) { }
108 /// Tag to disengage optional objects.
109 constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
113 * @brief Exception class thrown when a disengaged optional object is
115 * @ingroup exceptions
117 class bad_optional_access : public logic_error
120 bad_optional_access() : logic_error("bad optional access") { }
122 // XXX This constructor is non-standard. Should not be inline
123 explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
125 virtual ~bad_optional_access() noexcept = default;
129 __throw_bad_optional_access(const char*)
130 __attribute__((__noreturn__));
132 // XXX Does not belong here.
134 __throw_bad_optional_access(const char* __s)
135 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
137 template<typename _Tp, typename = void>
138 struct _Has_addressof_mem : std::false_type { };
140 template<typename _Tp>
141 struct _Has_addressof_mem<_Tp,
142 __void_t<decltype( std::declval<const _Tp&>().operator&() )>
144 : std::true_type { };
146 template<typename _Tp, typename = void>
147 struct _Has_addressof_free : std::false_type { };
149 template<typename _Tp>
150 struct _Has_addressof_free<_Tp,
151 __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
153 : std::true_type { };
156 * @brief Trait that detects the presence of an overloaded unary operator&.
158 * Practically speaking this detects the presence of such an operator when
159 * called on a const-qualified lvalue (i.e.
160 * declval<_Tp * const&>().operator&()).
162 template<typename _Tp>
163 struct _Has_addressof
164 : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
168 * @brief An overload that attempts to take the address of an lvalue as a
169 * constant expression. Falls back to __addressof in the presence of an
170 * overloaded addressof operator (unary operator&), in which case the call
171 * will not be a constant expression.
173 template<typename _Tp, enable_if_t<!_Has_addressof<_Tp>::value, int>...>
174 constexpr _Tp* __constexpr_addressof(_Tp& __t)
178 * @brief Fallback overload that defers to __addressof.
180 template<typename _Tp, enable_if_t<_Has_addressof<_Tp>::value, int>...>
181 inline _Tp* __constexpr_addressof(_Tp& __t)
182 { return std::__addressof(__t); }
185 * @brief Class template that holds the necessary state for @ref optional
186 * and that has the responsibility for construction and the special members.
188 * Such a separate base class template is necessary in order to
189 * conditionally enable the special members (e.g. copy/move constructors).
190 * Note that this means that @ref _Optional_base implements the
191 * functionality for copy and move assignment, but not for converting
194 * @see optional, _Enable_special_members
196 template<typename _Tp, bool _ShouldProvideDestructor =
197 !is_trivially_destructible<_Tp>::value>
201 // Remove const to avoid prohibition of reusing object storage for
202 // const-qualified types in [3.8/9]. This is strictly internal
203 // and even optional itself is oblivious to it.
204 using _Stored_type = remove_const_t<_Tp>;
207 // [X.Y.4.1] Constructors.
209 // Constructors for disengaged optionals.
210 constexpr _Optional_base() noexcept
213 constexpr _Optional_base(nullopt_t) noexcept
214 : _Optional_base{} { }
216 // Constructors for engaged optionals.
217 constexpr _Optional_base(const _Tp& __t)
218 : _M_payload(__t), _M_engaged(true) { }
220 constexpr _Optional_base(_Tp&& __t)
221 : _M_payload(std::move(__t)), _M_engaged(true) { }
223 template<typename... _Args>
224 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
225 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
227 template<typename _Up, typename... _Args,
228 enable_if_t<is_constructible<_Tp,
229 initializer_list<_Up>&,
232 constexpr explicit _Optional_base(in_place_t,
233 initializer_list<_Up> __il,
235 : _M_payload(__il, std::forward<_Args>(__args)...),
238 // Copy and move constructors.
239 _Optional_base(const _Optional_base& __other)
241 if (__other._M_engaged)
242 this->_M_construct(__other._M_get());
245 _Optional_base(_Optional_base&& __other)
246 noexcept(is_nothrow_move_constructible<_Tp>())
248 if (__other._M_engaged)
249 this->_M_construct(std::move(__other._M_get()));
252 // [X.Y.4.3] (partly) Assignment.
254 operator=(const _Optional_base& __other)
256 if (this->_M_engaged && __other._M_engaged)
257 this->_M_get() = __other._M_get();
260 if (__other._M_engaged)
261 this->_M_construct(__other._M_get());
270 operator=(_Optional_base&& __other)
271 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
272 is_nothrow_move_assignable<_Tp>>())
274 if (this->_M_engaged && __other._M_engaged)
275 this->_M_get() = std::move(__other._M_get());
278 if (__other._M_engaged)
279 this->_M_construct(std::move(__other._M_get()));
286 // [X.Y.4.2] Destructor.
289 if (this->_M_engaged)
290 this->_M_payload.~_Stored_type();
293 // The following functionality is also needed by optional, hence the
294 // protected accessibility.
296 constexpr bool _M_is_engaged() const noexcept
297 { return this->_M_engaged; }
299 // The _M_get operations have _M_engaged as a precondition.
302 { return _M_payload; }
305 _M_get() const noexcept
306 { return _M_payload; }
308 // The _M_construct operation has !_M_engaged as a precondition
309 // while _M_destruct has _M_engaged as a precondition.
310 template<typename... _Args>
312 _M_construct(_Args&&... __args)
313 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
315 ::new (std::__addressof(this->_M_payload))
316 _Stored_type(std::forward<_Args>(__args)...);
317 this->_M_engaged = true;
323 this->_M_engaged = false;
324 this->_M_payload.~_Stored_type();
327 // _M_reset is a 'safe' operation with no precondition.
331 if (this->_M_engaged)
336 struct _Empty_byte { };
338 _Empty_byte _M_empty;
339 _Stored_type _M_payload;
341 bool _M_engaged = false;
344 /// Partial specialization that is exactly identical to the primary template
345 /// save for not providing a destructor, to fulfill triviality requirements.
346 template<typename _Tp>
347 class _Optional_base<_Tp, false>
350 using _Stored_type = remove_const_t<_Tp>;
353 constexpr _Optional_base() noexcept
356 constexpr _Optional_base(nullopt_t) noexcept
357 : _Optional_base{} { }
359 constexpr _Optional_base(const _Tp& __t)
360 : _M_payload(__t), _M_engaged(true) { }
362 constexpr _Optional_base(_Tp&& __t)
363 : _M_payload(std::move(__t)), _M_engaged(true) { }
365 template<typename... _Args>
366 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
367 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
369 template<typename _Up, typename... _Args,
370 enable_if_t<is_constructible<_Tp,
371 initializer_list<_Up>&,
374 constexpr explicit _Optional_base(in_place_t,
375 initializer_list<_Up> __il,
377 : _M_payload(__il, std::forward<_Args>(__args)...),
380 _Optional_base(const _Optional_base& __other)
382 if (__other._M_engaged)
383 this->_M_construct(__other._M_get());
386 _Optional_base(_Optional_base&& __other)
387 noexcept(is_nothrow_move_constructible<_Tp>())
389 if (__other._M_engaged)
390 this->_M_construct(std::move(__other._M_get()));
394 operator=(const _Optional_base& __other)
396 if (this->_M_engaged && __other._M_engaged)
397 this->_M_get() = __other._M_get();
400 if (__other._M_engaged)
401 this->_M_construct(__other._M_get());
409 operator=(_Optional_base&& __other)
410 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
411 is_nothrow_move_assignable<_Tp>>())
413 if (this->_M_engaged && __other._M_engaged)
414 this->_M_get() = std::move(__other._M_get());
417 if (__other._M_engaged)
418 this->_M_construct(std::move(__other._M_get()));
426 // ~_Optional_base() noexcept = default;
429 constexpr bool _M_is_engaged() const noexcept
430 { return this->_M_engaged; }
434 { return _M_payload; }
437 _M_get() const noexcept
438 { return _M_payload; }
440 template<typename... _Args>
442 _M_construct(_Args&&... __args)
443 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
445 ::new (std::__addressof(this->_M_payload))
446 _Stored_type(std::forward<_Args>(__args)...);
447 this->_M_engaged = true;
453 this->_M_engaged = false;
454 this->_M_payload.~_Stored_type();
460 if (this->_M_engaged)
465 struct _Empty_byte { };
468 _Empty_byte _M_empty;
469 _Stored_type _M_payload;
471 bool _M_engaged = false;
474 template<typename _Tp>
478 struct __is_optional_impl : false_type
481 template<typename _Tp>
482 struct __is_optional_impl<optional<_Tp>> : true_type
485 template<typename _Tp>
487 : public __is_optional_impl<std::remove_cv_t<std::remove_reference_t<_Tp>>>
492 * @brief Class template for optional values.
494 template<typename _Tp>
496 : private _Optional_base<_Tp>,
497 private _Enable_copy_move<
499 is_copy_constructible<_Tp>::value,
501 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
503 is_move_constructible<_Tp>::value,
505 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
509 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
510 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
511 __not_<is_reference<_Tp>>>(),
512 "Invalid instantiation of optional<T>");
515 using _Base = _Optional_base<_Tp>;
518 using value_type = _Tp;
520 // _Optional_base has the responsibility for construction.
523 constexpr optional() = default;
524 // Converting constructors for engaged optionals.
525 template <typename _Up,
527 __not_<is_same<_Tp, _Up>>,
528 is_constructible<_Tp, _Up&&>,
529 is_convertible<_Up&&, _Tp>
530 >::value, bool> = true>
531 constexpr optional(_Up&& __t)
532 : _Base(_Tp(std::forward<_Up>(__t))) { }
534 template <typename _Up,
536 __not_<is_same<_Tp, _Up>>,
537 is_constructible<_Tp, _Up&&>,
538 __not_<is_convertible<_Up&&, _Tp>>
539 >::value, bool> = false>
540 explicit constexpr optional(_Up&& __t)
541 : _Base(_Tp(std::forward<_Up>(__t))) { }
543 template <typename _Up,
545 __not_<is_same<_Tp, _Up>>,
546 __not_<is_constructible<
547 _Tp, const optional<_Up>&>>,
548 __not_<is_convertible<
549 const optional<_Up>&, _Tp>>,
550 is_constructible<_Tp, const _Up&>,
551 is_convertible<const _Up&, _Tp>
552 >::value, bool> = true>
553 constexpr optional(const optional<_Up>& __t)
554 : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
556 template <typename _Up,
558 __not_<is_same<_Tp, _Up>>,
559 __not_<is_constructible<
560 _Tp, const optional<_Up>&>>,
561 __not_<is_convertible<
562 const optional<_Up>&, _Tp>>,
563 is_constructible<_Tp, const _Up&>,
564 __not_<is_convertible<const _Up&, _Tp>>
565 >::value, bool> = false>
566 explicit constexpr optional(const optional<_Up>& __t)
567 : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
569 template <typename _Up,
571 __not_<is_same<_Tp, _Up>>,
572 __not_<is_constructible<
573 _Tp, optional<_Up>&&>>,
574 __not_<is_convertible<
575 optional<_Up>&&, _Tp>>,
576 is_constructible<_Tp, _Up&&>,
577 is_convertible<_Up&&, _Tp>
578 >::value, bool> = true>
579 constexpr optional(optional<_Up>&& __t)
580 : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
582 template <typename _Up,
584 __not_<is_same<_Tp, _Up>>,
585 __not_<is_constructible<
586 _Tp, optional<_Up>&&>>,
587 __not_<is_convertible<
588 optional<_Up>&&, _Tp>>,
589 is_constructible<_Tp, _Up&&>,
590 __not_<is_convertible<_Up&&, _Tp>>
591 >::value, bool> = false>
592 explicit constexpr optional(optional<_Up>&& __t)
593 : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
595 // [X.Y.4.3] (partly) Assignment.
597 operator=(nullopt_t) noexcept
603 template<typename _Up,
605 __not_<is_same<_Up, nullopt_t>>,
606 __not_<__is_optional<_Up>>>::value,
611 static_assert(__and_<is_constructible<_Tp, _Up>,
612 is_assignable<_Tp&, _Up>>(),
613 "Cannot assign to value type from argument");
615 if (this->_M_is_engaged())
616 this->_M_get() = std::forward<_Up>(__u);
618 this->_M_construct(std::forward<_Up>(__u));
623 template<typename _Up,
625 __not_<is_same<_Tp, _Up>>>::value,
628 operator=(const optional<_Up>& __u)
630 static_assert(__and_<is_constructible<_Tp, _Up>,
631 is_assignable<_Tp&, _Up>>(),
632 "Cannot assign to value type from argument");
636 if (this->_M_is_engaged())
637 this->_M_get() = *__u;
639 this->_M_construct(*__u);
648 template<typename _Up,
650 __not_<is_same<_Tp, _Up>>>::value,
653 operator=(optional<_Up>&& __u)
655 static_assert(__and_<is_constructible<_Tp, _Up>,
656 is_assignable<_Tp&, _Up>>(),
657 "Cannot assign to value type from argument");
661 if (this->_M_is_engaged())
662 this->_M_get() = std::move(*__u);
664 this->_M_construct(std::move(*__u));
674 template<typename... _Args>
676 emplace(_Args&&... __args)
678 static_assert(is_constructible<_Tp, _Args&&...>(),
679 "Cannot emplace value type from arguments");
682 this->_M_construct(std::forward<_Args>(__args)...);
685 template<typename _Up, typename... _Args>
686 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
688 emplace(initializer_list<_Up> __il, _Args&&... __args)
691 this->_M_construct(__il, std::forward<_Args>(__args)...);
694 // [X.Y.4.2] Destructor is implicit, implemented in _Optional_base.
698 swap(optional& __other)
699 noexcept(is_nothrow_move_constructible<_Tp>()
700 && noexcept(swap(declval<_Tp&>(), declval<_Tp&>())))
704 if (this->_M_is_engaged() && __other._M_is_engaged())
705 swap(this->_M_get(), __other._M_get());
706 else if (this->_M_is_engaged())
708 __other._M_construct(std::move(this->_M_get()));
711 else if (__other._M_is_engaged())
713 this->_M_construct(std::move(__other._M_get()));
714 __other._M_destruct();
718 // [X.Y.4.5] Observers.
721 { return __constexpr_addressof(this->_M_get()); }
725 { return std::__addressof(this->_M_get()); }
729 { return this->_M_get(); }
733 { return this->_M_get(); }
737 { return std::move(this->_M_get()); }
739 constexpr const _Tp&&
741 { return std::move(this->_M_get()); }
743 constexpr explicit operator bool() const noexcept
744 { return this->_M_is_engaged(); }
749 return this->_M_is_engaged()
751 : (__throw_bad_optional_access("Attempt to access value of a "
752 "disengaged optional object"),
759 return this->_M_is_engaged()
761 : (__throw_bad_optional_access("Attempt to access value of a "
762 "disengaged optional object"),
769 return this->_M_is_engaged()
770 ? std::move(this->_M_get())
771 : (__throw_bad_optional_access("Attempt to access value of a "
772 "disengaged optional object"),
773 std::move(this->_M_get()));
776 constexpr const _Tp&&
779 return this->_M_is_engaged()
780 ? std::move(this->_M_get())
781 : (__throw_bad_optional_access("Attempt to access value of a "
782 "disengaged optional object"),
783 std::move(this->_M_get()));
786 template<typename _Up>
788 value_or(_Up&& __u) const&
790 static_assert(__and_<is_copy_constructible<_Tp>,
791 is_convertible<_Up&&, _Tp>>(),
792 "Cannot return value");
794 return this->_M_is_engaged()
796 : static_cast<_Tp>(std::forward<_Up>(__u));
799 template<typename _Up>
801 value_or(_Up&& __u) &&
803 static_assert(__and_<is_move_constructible<_Tp>,
804 is_convertible<_Up&&, _Tp>>(),
805 "Cannot return value" );
807 return this->_M_is_engaged()
808 ? std::move(this->_M_get())
809 : static_cast<_Tp>(std::forward<_Up>(__u));
813 // [X.Y.8] Comparisons between optional values.
814 template<typename _Tp>
816 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
818 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
819 && (!__lhs || *__lhs == *__rhs);
822 template<typename _Tp>
824 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
825 { return !(__lhs == __rhs); }
827 template<typename _Tp>
829 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
831 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
834 template<typename _Tp>
836 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
837 { return __rhs < __lhs; }
839 template<typename _Tp>
841 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
842 { return !(__rhs < __lhs); }
844 template<typename _Tp>
846 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
847 { return !(__lhs < __rhs); }
849 // [X.Y.9] Comparisons with nullopt.
850 template<typename _Tp>
852 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
855 template<typename _Tp>
857 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
860 template<typename _Tp>
862 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
863 { return static_cast<bool>(__lhs); }
865 template<typename _Tp>
867 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
868 { return static_cast<bool>(__rhs); }
870 template<typename _Tp>
872 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
875 template<typename _Tp>
877 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
878 { return static_cast<bool>(__rhs); }
880 template<typename _Tp>
882 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
883 { return static_cast<bool>(__lhs); }
885 template<typename _Tp>
887 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
890 template<typename _Tp>
892 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
895 template<typename _Tp>
897 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
900 template<typename _Tp>
902 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
905 template<typename _Tp>
907 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
910 // [X.Y.10] Comparisons with value type.
911 template<typename _Tp>
913 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
914 { return __lhs && *__lhs == __rhs; }
916 template<typename _Tp>
918 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
919 { return __rhs && __lhs == *__rhs; }
921 template<typename _Tp>
923 operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
924 { return !__lhs || !(*__lhs == __rhs); }
926 template<typename _Tp>
928 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
929 { return !__rhs || !(__lhs == *__rhs); }
931 template<typename _Tp>
933 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
934 { return !__lhs || *__lhs < __rhs; }
936 template<typename _Tp>
938 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
939 { return __rhs && __lhs < *__rhs; }
941 template<typename _Tp>
943 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
944 { return __lhs && __rhs < *__lhs; }
946 template<typename _Tp>
948 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
949 { return !__rhs || *__rhs < __lhs; }
951 template<typename _Tp>
953 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
954 { return !__lhs || !(__rhs < *__lhs); }
956 template<typename _Tp>
958 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
959 { return __rhs && !(*__rhs < __lhs); }
961 template<typename _Tp>
963 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
964 { return __lhs && !(*__lhs < __rhs); }
966 template<typename _Tp>
968 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
969 { return !__rhs || !(__lhs < *__rhs); }
972 template<typename _Tp>
974 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
975 noexcept(noexcept(__lhs.swap(__rhs)))
976 { __lhs.swap(__rhs); }
978 template<typename _Tp>
979 constexpr optional<decay_t<_Tp>>
980 make_optional(_Tp&& __t)
981 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
984 _GLIBCXX_END_NAMESPACE_VERSION
985 } // namespace fundamentals_v1
989 template<typename _Tp>
990 struct hash<experimental::optional<_Tp>>
992 using result_type = size_t;
993 using argument_type = experimental::optional<_Tp>;
996 operator()(const experimental::optional<_Tp>& __t) const
997 noexcept(noexcept(hash<_Tp> {}(*__t)))
999 // We pick an arbitrary hash for disengaged optionals which hopefully
1000 // usual values of _Tp won't typically hash to.
1001 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1002 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1009 #endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL