1 // <optional> -*- C++ -*-
3 // Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/optional
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_OPTIONAL
30 #define _GLIBCXX_OPTIONAL 1
32 #if __cplusplus <= 201402L
33 # include <bits/c++17_warning.h>
37 #include <type_traits>
40 #include <initializer_list>
41 #include <bits/functexcept.h>
42 #include <bits/functional_hash.h>
43 #include <bits/enable_special_members.h>
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 * @addtogroup utilities
54 template<typename _Tp>
57 /// Tag type to disengage optional objects.
60 // Do not user-declare default constructor at all for
61 // optional_value = {} syntax to work.
62 // nullopt_t() = delete;
64 // Used for constructing nullopt.
65 enum class _Construct { _Token };
67 // Must be constexpr for nullopt_t to be literal.
68 explicit constexpr nullopt_t(_Construct) { }
71 /// Tag to disengage optional objects.
72 inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
75 * @brief Exception class thrown when a disengaged optional object is
79 class bad_optional_access : public exception
82 bad_optional_access() { }
83 virtual const char* what() const noexcept override
84 {return "bad optional access";}
86 virtual ~bad_optional_access() noexcept = default;
90 __throw_bad_optional_access()
91 __attribute__((__noreturn__));
93 // XXX Does not belong here.
95 __throw_bad_optional_access()
96 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
99 // Payload for constexpr optionals.
100 template <typename _Tp,
101 bool /*_TrivialCopyMove*/ =
102 is_trivially_copy_constructible<_Tp>::value
103 && is_trivially_move_constructible<_Tp>::value,
104 bool /*_ShouldProvideDestructor*/ =
105 is_trivially_destructible<_Tp>::value>
106 struct _Optional_payload
108 constexpr _Optional_payload()
111 template<typename... _Args>
112 constexpr _Optional_payload(in_place_t, _Args&&... __args)
113 : _M_payload(std::forward<_Args>(__args)...),
117 template<typename _Up, typename... _Args>
118 constexpr _Optional_payload(std::initializer_list<_Up> __il,
120 : _M_payload(__il, std::forward<_Args>(__args)...),
123 template <class _Up> struct __ctor_tag {};
125 constexpr _Optional_payload(__ctor_tag<bool>,
127 : _M_payload(__other),
131 constexpr _Optional_payload(__ctor_tag<void>)
135 constexpr _Optional_payload(__ctor_tag<bool>, _Tp&& __other)
136 : _M_payload(std::move(__other)),
140 constexpr _Optional_payload(bool __engaged,
141 const _Optional_payload& __other)
142 : _Optional_payload(__engaged ?
143 _Optional_payload(__ctor_tag<bool>{},
144 __other._M_payload) :
145 _Optional_payload(__ctor_tag<void>{}))
148 constexpr _Optional_payload(bool __engaged,
149 _Optional_payload&& __other)
150 : _Optional_payload(__engaged
151 ? _Optional_payload(__ctor_tag<bool>{},
152 std::move(__other._M_payload))
153 : _Optional_payload(__ctor_tag<void>{}))
156 using _Stored_type = remove_const_t<_Tp>;
157 struct _Empty_byte { };
159 _Empty_byte _M_empty;
160 _Stored_type _M_payload;
162 bool _M_engaged = false;
165 // Payload for non-constexpr optionals with non-trivial destructor.
166 template <typename _Tp>
167 struct _Optional_payload<_Tp, false, false>
169 constexpr _Optional_payload()
172 template <typename... _Args>
173 constexpr _Optional_payload(in_place_t, _Args&&... __args)
174 : _M_payload(std::forward<_Args>(__args)...),
177 template<typename _Up, typename... _Args>
178 constexpr _Optional_payload(std::initializer_list<_Up> __il,
180 : _M_payload(__il, std::forward<_Args>(__args)...),
183 _Optional_payload(bool __engaged, const _Optional_payload& __other)
184 : _Optional_payload(__other)
188 _Optional_payload(bool __engaged, _Optional_payload&& __other)
189 : _Optional_payload(std::move(__other))
192 constexpr _Optional_payload(const _Optional_payload& __other)
194 if (__other._M_engaged)
195 this->_M_construct(__other._M_payload);
198 constexpr _Optional_payload(_Optional_payload&& __other)
200 if (__other._M_engaged)
201 this->_M_construct(std::move(__other._M_payload));
204 using _Stored_type = remove_const_t<_Tp>;
205 struct _Empty_byte { };
207 _Empty_byte _M_empty;
208 _Stored_type _M_payload;
210 bool _M_engaged = false;
215 _M_payload.~_Stored_type();
218 template<typename... _Args>
220 _M_construct(_Args&&... __args)
221 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
223 ::new ((void *) std::__addressof(this->_M_payload))
224 _Stored_type(std::forward<_Args>(__args)...);
225 this->_M_engaged = true;
229 // Payload for non-constexpr optionals with trivial destructor.
230 template <typename _Tp>
231 struct _Optional_payload<_Tp, false, true>
233 constexpr _Optional_payload()
236 template <typename... _Args>
237 constexpr _Optional_payload(in_place_t, _Args&&... __args)
238 : _M_payload(std::forward<_Args>(__args)...),
241 template<typename _Up, typename... _Args>
242 constexpr _Optional_payload(std::initializer_list<_Up> __il,
244 : _M_payload(__il, std::forward<_Args>(__args)...),
247 _Optional_payload(bool __engaged, const _Optional_payload& __other)
248 : _Optional_payload(__other)
252 _Optional_payload(bool __engaged, _Optional_payload&& __other)
253 : _Optional_payload(std::move(__other))
256 constexpr _Optional_payload(const _Optional_payload& __other)
258 if (__other._M_engaged)
259 this->_M_construct(__other._M_payload);
262 constexpr _Optional_payload(_Optional_payload&& __other)
264 if (__other._M_engaged)
265 this->_M_construct(std::move(__other._M_payload));
268 using _Stored_type = remove_const_t<_Tp>;
269 struct _Empty_byte { };
271 _Empty_byte _M_empty;
272 _Stored_type _M_payload;
274 bool _M_engaged = false;
276 template<typename... _Args>
278 _M_construct(_Args&&... __args)
279 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
281 ::new ((void *) std::__addressof(this->_M_payload))
282 _Stored_type(std::forward<_Args>(__args)...);
283 this->_M_engaged = true;
288 * @brief Class template that holds the necessary state for @ref optional
289 * and that has the responsibility for construction and the special members.
291 * Such a separate base class template is necessary in order to
292 * conditionally enable the special members (e.g. copy/move constructors).
293 * Note that this means that @ref _Optional_base implements the
294 * functionality for copy and move assignment, but not for converting
297 * @see optional, _Enable_special_members
299 template<typename _Tp>
303 // Remove const to avoid prohibition of reusing object storage for
304 // const-qualified types in [3.8/9]. This is strictly internal
305 // and even optional itself is oblivious to it.
306 using _Stored_type = remove_const_t<_Tp>;
310 // Constructors for disengaged optionals.
311 constexpr _Optional_base() noexcept
314 constexpr _Optional_base(nullopt_t) noexcept
317 // Constructors for engaged optionals.
318 template<typename... _Args,
319 enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
320 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
321 : _M_payload(in_place,
322 std::forward<_Args>(__args)...) { }
324 template<typename _Up, typename... _Args,
325 enable_if_t<is_constructible_v<_Tp,
326 initializer_list<_Up>&,
327 _Args&&...>, bool> = false>
328 constexpr explicit _Optional_base(in_place_t,
329 initializer_list<_Up> __il,
331 : _M_payload(in_place,
332 __il, std::forward<_Args>(__args)...)
335 // Copy and move constructors.
336 constexpr _Optional_base(const _Optional_base& __other)
337 : _M_payload(__other._M_payload._M_engaged,
341 constexpr _Optional_base(_Optional_base&& __other)
342 noexcept(is_nothrow_move_constructible<_Tp>())
343 : _M_payload(__other._M_payload._M_engaged,
344 std::move(__other._M_payload))
347 // Assignment operators.
349 operator=(const _Optional_base& __other)
351 if (this->_M_payload._M_engaged && __other._M_payload._M_engaged)
352 this->_M_get() = __other._M_get();
355 if (__other._M_payload._M_engaged)
356 this->_M_construct(__other._M_get());
365 operator=(_Optional_base&& __other)
366 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
367 is_nothrow_move_assignable<_Tp>>())
369 if (this->_M_payload._M_engaged && __other._M_payload._M_engaged)
370 this->_M_get() = std::move(__other._M_get());
373 if (__other._M_payload._M_engaged)
374 this->_M_construct(std::move(__other._M_get()));
380 // The following functionality is also needed by optional, hence the
381 // protected accessibility.
383 constexpr bool _M_is_engaged() const noexcept
384 { return this->_M_payload._M_engaged; }
386 // The _M_get operations have _M_engaged as a precondition.
389 { return this->_M_payload._M_payload; }
392 _M_get() const noexcept
393 { return this->_M_payload._M_payload; }
395 // The _M_construct operation has !_M_engaged as a precondition
396 // while _M_destruct has _M_engaged as a precondition.
397 template<typename... _Args>
399 _M_construct(_Args&&... __args)
400 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
402 ::new (std::__addressof(this->_M_payload._M_payload))
403 _Stored_type(std::forward<_Args>(__args)...);
404 this->_M_payload._M_engaged = true;
410 this->_M_payload._M_engaged = false;
411 this->_M_payload._M_payload.~_Stored_type();
414 // _M_reset is a 'safe' operation with no precondition.
418 if (this->_M_payload._M_engaged)
423 _Optional_payload<_Tp> _M_payload;
426 template<typename _Tp>
429 template<typename _Tp, typename _Up>
430 using __converts_from_optional =
431 __or_<is_constructible<_Tp, const optional<_Up>&>,
432 is_constructible<_Tp, optional<_Up>&>,
433 is_constructible<_Tp, const optional<_Up>&&>,
434 is_constructible<_Tp, optional<_Up>&&>,
435 is_convertible<const optional<_Up>&, _Tp>,
436 is_convertible<optional<_Up>&, _Tp>,
437 is_convertible<const optional<_Up>&&, _Tp>,
438 is_convertible<optional<_Up>&&, _Tp>>;
440 template<typename _Tp, typename _Up>
441 using __assigns_from_optional =
442 __or_<is_assignable<_Tp&, const optional<_Up>&>,
443 is_assignable<_Tp&, optional<_Up>&>,
444 is_assignable<_Tp&, const optional<_Up>&&>,
445 is_assignable<_Tp&, optional<_Up>&&>>;
448 * @brief Class template for optional values.
450 template<typename _Tp>
452 : private _Optional_base<_Tp>,
453 private _Enable_copy_move<
455 is_copy_constructible<_Tp>::value,
457 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
459 is_move_constructible<_Tp>::value,
461 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
465 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
466 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
467 __not_<is_reference<_Tp>>>(),
468 "Invalid instantiation of optional<T>");
471 using _Base = _Optional_base<_Tp>;
474 using value_type = _Tp;
476 constexpr optional() = default;
478 constexpr optional(nullopt_t) noexcept
481 // Converting constructors for engaged optionals.
482 template <typename _Up = _Tp,
484 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
485 __not_<is_same<in_place_t, decay_t<_Up>>>,
486 is_constructible<_Tp, _Up&&>,
487 is_convertible<_Up&&, _Tp>
488 >::value, bool> = true>
489 constexpr optional(_Up&& __t)
490 : _Base(std::in_place, std::forward<_Up>(__t)) { }
492 template <typename _Up = _Tp,
494 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
495 __not_<is_same<in_place_t, decay_t<_Up>>>,
496 is_constructible<_Tp, _Up&&>,
497 __not_<is_convertible<_Up&&, _Tp>>
498 >::value, bool> = false>
499 explicit constexpr optional(_Up&& __t)
500 : _Base(std::in_place, std::forward<_Up>(__t)) { }
502 template <typename _Up,
504 __not_<is_same<_Tp, _Up>>,
505 is_constructible<_Tp, const _Up&>,
506 is_convertible<const _Up&, _Tp>,
507 __not_<__converts_from_optional<_Tp, _Up>>
508 >::value, bool> = true>
509 constexpr optional(const optional<_Up>& __t)
515 template <typename _Up,
517 __not_<is_same<_Tp, _Up>>,
518 is_constructible<_Tp, const _Up&>,
519 __not_<is_convertible<const _Up&, _Tp>>,
520 __not_<__converts_from_optional<_Tp, _Up>>
521 >::value, bool> = false>
522 explicit constexpr optional(const optional<_Up>& __t)
528 template <typename _Up,
530 __not_<is_same<_Tp, _Up>>,
531 is_constructible<_Tp, _Up&&>,
532 is_convertible<_Up&&, _Tp>,
533 __not_<__converts_from_optional<_Tp, _Up>>
534 >::value, bool> = true>
535 constexpr optional(optional<_Up>&& __t)
538 emplace(std::move(*__t));
541 template <typename _Up,
543 __not_<is_same<_Tp, _Up>>,
544 is_constructible<_Tp, _Up&&>,
545 __not_<is_convertible<_Up&&, _Tp>>,
546 __not_<__converts_from_optional<_Tp, _Up>>
547 >::value, bool> = false>
548 explicit constexpr optional(optional<_Up>&& __t)
551 emplace(std::move(*__t));
554 template<typename... _Args,
555 enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
556 explicit constexpr optional(in_place_t, _Args&&... __args)
557 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
559 template<typename _Up, typename... _Args,
560 enable_if_t<is_constructible_v<_Tp,
561 initializer_list<_Up>&,
562 _Args&&...>, bool> = false>
563 explicit constexpr optional(in_place_t,
564 initializer_list<_Up> __il,
566 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
568 // Assignment operators.
570 operator=(nullopt_t) noexcept
576 template<typename _Up = _Tp>
578 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
579 is_constructible<_Tp, _Up>,
580 __not_<__and_<is_scalar<_Tp>,
581 is_same<_Tp, decay_t<_Up>>>>,
582 is_assignable<_Tp&, _Up>>::value,
586 if (this->_M_is_engaged())
587 this->_M_get() = std::forward<_Up>(__u);
589 this->_M_construct(std::forward<_Up>(__u));
594 template<typename _Up>
596 __not_<is_same<_Tp, _Up>>,
597 is_constructible<_Tp, const _Up&>,
598 is_assignable<_Tp&, _Up>,
599 __not_<__converts_from_optional<_Tp, _Up>>,
600 __not_<__assigns_from_optional<_Tp, _Up>>
603 operator=(const optional<_Up>& __u)
607 if (this->_M_is_engaged())
608 this->_M_get() = *__u;
610 this->_M_construct(*__u);
619 template<typename _Up>
621 __not_<is_same<_Tp, _Up>>,
622 is_constructible<_Tp, _Up>,
623 is_assignable<_Tp&, _Up>,
624 __not_<__converts_from_optional<_Tp, _Up>>,
625 __not_<__assigns_from_optional<_Tp, _Up>>
628 operator=(optional<_Up>&& __u)
632 if (this->_M_is_engaged())
633 this->_M_get() = std::move(*__u);
635 this->_M_construct(std::move(*__u));
645 template<typename... _Args>
646 enable_if_t<is_constructible<_Tp, _Args&&...>::value, _Tp&>
647 emplace(_Args&&... __args)
650 this->_M_construct(std::forward<_Args>(__args)...);
651 return this->_M_get();
654 template<typename _Up, typename... _Args>
655 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
656 _Args&&...>::value, _Tp&>
657 emplace(initializer_list<_Up> __il, _Args&&... __args)
660 this->_M_construct(__il, std::forward<_Args>(__args)...);
661 return this->_M_get();
664 // Destructor is implicit, implemented in _Optional_base.
668 swap(optional& __other)
669 noexcept(is_nothrow_move_constructible<_Tp>()
670 && is_nothrow_swappable_v<_Tp>)
674 if (this->_M_is_engaged() && __other._M_is_engaged())
675 swap(this->_M_get(), __other._M_get());
676 else if (this->_M_is_engaged())
678 __other._M_construct(std::move(this->_M_get()));
681 else if (__other._M_is_engaged())
683 this->_M_construct(std::move(__other._M_get()));
684 __other._M_destruct();
691 { return std::__addressof(this->_M_get()); }
695 { return std::__addressof(this->_M_get()); }
699 { return this->_M_get(); }
703 { return this->_M_get(); }
707 { return std::move(this->_M_get()); }
709 constexpr const _Tp&&
711 { return std::move(this->_M_get()); }
713 constexpr explicit operator bool() const noexcept
714 { return this->_M_is_engaged(); }
716 constexpr bool has_value() const noexcept
717 { return this->_M_is_engaged(); }
722 return this->_M_is_engaged()
724 : (__throw_bad_optional_access(),
731 return this->_M_is_engaged()
733 : (__throw_bad_optional_access(),
740 return this->_M_is_engaged()
741 ? std::move(this->_M_get())
742 : (__throw_bad_optional_access(),
743 std::move(this->_M_get()));
746 constexpr const _Tp&&
749 return this->_M_is_engaged()
750 ? std::move(this->_M_get())
751 : (__throw_bad_optional_access(),
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, typename _Up>
790 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
791 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
793 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
794 && (!__lhs || *__lhs == *__rhs);
797 template<typename _Tp, typename _Up>
799 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
800 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
802 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
803 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
806 template<typename _Tp, typename _Up>
808 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
809 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
811 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
814 template<typename _Tp, typename _Up>
816 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
817 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
819 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
822 template<typename _Tp, typename _Up>
824 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
825 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
827 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
830 template<typename _Tp, typename _Up>
832 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
833 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
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, typename _Up>
902 operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
903 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
904 { return __lhs && *__lhs == __rhs; }
906 template<typename _Tp, typename _Up>
908 operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
909 -> __optional_relop_t<decltype(declval<_Up>() == declval<_Tp>())>
910 { return __rhs && __lhs == *__rhs; }
912 template<typename _Tp, typename _Up>
914 operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
915 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
916 { return !__lhs || *__lhs != __rhs; }
918 template<typename _Tp, typename _Up>
920 operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
921 -> __optional_relop_t<decltype(declval<_Up>() != declval<_Tp>())>
922 { return !__rhs || __lhs != *__rhs; }
924 template<typename _Tp, typename _Up>
926 operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
927 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
928 { return !__lhs || *__lhs < __rhs; }
930 template<typename _Tp, typename _Up>
932 operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
933 -> __optional_relop_t<decltype(declval<_Up>() < declval<_Tp>())>
934 { return __rhs && __lhs < *__rhs; }
936 template<typename _Tp, typename _Up>
938 operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
939 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
940 { return __lhs && *__lhs > __rhs; }
942 template<typename _Tp, typename _Up>
944 operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
945 -> __optional_relop_t<decltype(declval<_Up>() > declval<_Tp>())>
946 { return !__rhs || __lhs > *__rhs; }
948 template<typename _Tp, typename _Up>
950 operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
951 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
952 { return !__lhs || *__lhs <= __rhs; }
954 template<typename _Tp, typename _Up>
956 operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
957 -> __optional_relop_t<decltype(declval<_Up>() <= declval<_Tp>())>
958 { return __rhs && __lhs <= *__rhs; }
960 template<typename _Tp, typename _Up>
962 operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
963 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
964 { return __lhs && *__lhs >= __rhs; }
966 template<typename _Tp, typename _Up>
968 operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
969 -> __optional_relop_t<decltype(declval<_Up>() >= declval<_Tp>())>
970 { return !__rhs || __lhs >= *__rhs; }
972 // Swap and creation functions.
974 // _GLIBCXX_RESOLVE_LIB_DEFECTS
975 // 2748. swappable traits for optionals
976 template<typename _Tp>
977 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
978 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
979 noexcept(noexcept(__lhs.swap(__rhs)))
980 { __lhs.swap(__rhs); }
982 template<typename _Tp>
983 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
984 swap(optional<_Tp>&, optional<_Tp>&) = delete;
986 template<typename _Tp>
987 constexpr optional<decay_t<_Tp>>
988 make_optional(_Tp&& __t)
989 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
991 template<typename _Tp, typename ..._Args>
992 constexpr optional<_Tp>
993 make_optional(_Args&&... __args)
994 { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
996 template<typename _Tp, typename _Up, typename ..._Args>
997 constexpr optional<_Tp>
998 make_optional(initializer_list<_Up> __il, _Args&&... __args)
999 { return optional<_Tp> { in_place, __il, std::forward<_Args>(__args)... }; }
1003 template<typename _Tp, bool
1004 = __poison_hash<remove_const_t<_Tp>>::__enable_hash_call>
1005 struct __optional_hash_call_base
1008 operator()(const optional<_Tp>& __t) const
1009 noexcept(noexcept(hash<_Tp> {}(*__t)))
1011 // We pick an arbitrary hash for disengaged optionals which hopefully
1012 // usual values of _Tp won't typically hash to.
1013 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1014 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1018 template<typename _Tp>
1019 struct __optional_hash_call_base<_Tp, false> {};
1021 template<typename _Tp>
1022 struct hash<optional<_Tp>>
1023 : private __poison_hash<remove_const_t<_Tp>>,
1024 public __optional_hash_call_base<_Tp>
1026 using result_type = size_t;
1027 using argument_type = optional<_Tp>;
1032 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1034 _GLIBCXX_END_NAMESPACE_VERSION
1039 #endif // _GLIBCXX_OPTIONAL