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 // All subsequent [X.Y.n] references are against n3793.
57 template<typename _Tp>
61 /// Tag type for in-place construction.
62 struct in_place_t { };
64 /// Tag for in-place construction.
65 constexpr in_place_t in_place { };
68 /// Tag type to disengage optional objects.
71 // Do not user-declare default constructor at all for
72 // optional_value = {} syntax to work.
73 // nullopt_t() = delete;
75 // Used for constructing nullopt.
76 enum class _Construct { _Token };
78 // Must be constexpr for nullopt_t to be literal.
79 explicit constexpr nullopt_t(_Construct) { }
83 /// Tag to disengage optional objects.
84 constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
88 * @brief Exception class thrown when a disengaged optional object is
92 class bad_optional_access : public logic_error
95 bad_optional_access() : logic_error("bad optional access") { }
97 // XXX This constructor is non-standard. Should not be inline
98 explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
100 virtual ~bad_optional_access() noexcept = default;
104 __throw_bad_optional_access(const char*)
105 __attribute__((__noreturn__));
107 // XXX Does not belong here.
109 __throw_bad_optional_access(const char* __s)
110 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
112 template<typename _Tp, typename = void>
113 struct _Has_addressof_mem : std::false_type { };
115 template<typename _Tp>
116 struct _Has_addressof_mem<_Tp,
117 __void_t<decltype( std::declval<const _Tp&>().operator&() )>
119 : std::true_type { };
121 template<typename _Tp, typename = void>
122 struct _Has_addressof_free : std::false_type { };
124 template<typename _Tp>
125 struct _Has_addressof_free<_Tp,
126 __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
128 : std::true_type { };
131 * @brief Trait that detects the presence of an overloaded unary operator&.
133 * Practically speaking this detects the presence of such an operator when
134 * called on a const-qualified lvalue (i.e.
135 * declval<_Tp * const&>().operator&()).
137 template<typename _Tp>
138 struct _Has_addressof
139 : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
143 * @brief An overload that attempts to take the address of an lvalue as a
144 * constant expression. Falls back to __addressof in the presence of an
145 * overloaded addressof operator (unary operator&), in which case the call
146 * will not be a constant expression.
148 template<typename _Tp, enable_if_t<!_Has_addressof<_Tp>::value, int>...>
149 constexpr _Tp* __constexpr_addressof(_Tp& __t)
153 * @brief Fallback overload that defers to __addressof.
155 template<typename _Tp, enable_if_t<_Has_addressof<_Tp>::value, int>...>
156 inline _Tp* __constexpr_addressof(_Tp& __t)
157 { return std::__addressof(__t); }
160 * @brief Class template that holds the necessary state for @ref optional
161 * and that has the responsibility for construction and the special members.
163 * Such a separate base class template is necessary in order to
164 * conditionally enable the special members (e.g. copy/move constructors).
165 * Note that this means that @ref _Optional_base implements the
166 * functionality for copy and move assignment, but not for converting
169 * @see optional, _Enable_special_members
171 template<typename _Tp, bool _ShouldProvideDestructor =
172 !is_trivially_destructible<_Tp>::value>
176 // Remove const to avoid prohibition of reusing object storage for
177 // const-qualified types in [3.8/9]. This is strictly internal
178 // and even optional itself is oblivious to it.
179 using _Stored_type = remove_const_t<_Tp>;
182 // [X.Y.4.1] Constructors.
184 // Constructors for disengaged optionals.
185 constexpr _Optional_base() noexcept
188 constexpr _Optional_base(nullopt_t) noexcept
189 : _Optional_base{} { }
191 // Constructors for engaged optionals.
192 constexpr _Optional_base(const _Tp& __t)
193 : _M_payload(__t), _M_engaged(true) { }
195 constexpr _Optional_base(_Tp&& __t)
196 : _M_payload(std::move(__t)), _M_engaged(true) { }
198 template<typename... _Args>
199 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
200 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
202 template<typename _Up, typename... _Args,
203 enable_if_t<is_constructible<_Tp,
204 initializer_list<_Up>&,
207 constexpr explicit _Optional_base(in_place_t,
208 initializer_list<_Up> __il,
210 : _M_payload(__il, std::forward<_Args>(__args)...),
213 // Copy and move constructors.
214 _Optional_base(const _Optional_base& __other)
216 if (__other._M_engaged)
217 this->_M_construct(__other._M_get());
220 _Optional_base(_Optional_base&& __other)
221 noexcept(is_nothrow_move_constructible<_Tp>())
223 if (__other._M_engaged)
224 this->_M_construct(std::move(__other._M_get()));
227 // [X.Y.4.3] (partly) Assignment.
229 operator=(const _Optional_base& __other)
231 if (this->_M_engaged && __other._M_engaged)
232 this->_M_get() = __other._M_get();
235 if (__other._M_engaged)
236 this->_M_construct(__other._M_get());
245 operator=(_Optional_base&& __other)
246 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
247 is_nothrow_move_assignable<_Tp>>())
249 if (this->_M_engaged && __other._M_engaged)
250 this->_M_get() = std::move(__other._M_get());
253 if (__other._M_engaged)
254 this->_M_construct(std::move(__other._M_get()));
261 // [X.Y.4.2] Destructor.
264 if (this->_M_engaged)
265 this->_M_payload.~_Stored_type();
268 // The following functionality is also needed by optional, hence the
269 // protected accessibility.
271 constexpr bool _M_is_engaged() const noexcept
272 { return this->_M_engaged; }
274 // The _M_get operations have _M_engaged as a precondition.
277 { return _M_payload; }
280 _M_get() const noexcept
281 { return _M_payload; }
283 // The _M_construct operation has !_M_engaged as a precondition
284 // while _M_destruct has _M_engaged as a precondition.
285 template<typename... _Args>
287 _M_construct(_Args&&... __args)
288 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
290 ::new (std::__addressof(this->_M_payload))
291 _Stored_type(std::forward<_Args>(__args)...);
292 this->_M_engaged = true;
298 this->_M_engaged = false;
299 this->_M_payload.~_Stored_type();
302 // _M_reset is a 'safe' operation with no precondition.
306 if (this->_M_engaged)
311 struct _Empty_byte { };
313 _Empty_byte _M_empty;
314 _Stored_type _M_payload;
316 bool _M_engaged = false;
319 /// Partial specialization that is exactly identical to the primary template
320 /// save for not providing a destructor, to fulfill triviality requirements.
321 template<typename _Tp>
322 class _Optional_base<_Tp, false>
325 using _Stored_type = remove_const_t<_Tp>;
328 constexpr _Optional_base() noexcept
331 constexpr _Optional_base(nullopt_t) noexcept
332 : _Optional_base{} { }
334 constexpr _Optional_base(const _Tp& __t)
335 : _M_payload(__t), _M_engaged(true) { }
337 constexpr _Optional_base(_Tp&& __t)
338 : _M_payload(std::move(__t)), _M_engaged(true) { }
340 template<typename... _Args>
341 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
342 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
344 template<typename _Up, typename... _Args,
345 enable_if_t<is_constructible<_Tp,
346 initializer_list<_Up>&,
349 constexpr explicit _Optional_base(in_place_t,
350 initializer_list<_Up> __il,
352 : _M_payload(__il, std::forward<_Args>(__args)...),
355 _Optional_base(const _Optional_base& __other)
357 if (__other._M_engaged)
358 this->_M_construct(__other._M_get());
361 _Optional_base(_Optional_base&& __other)
362 noexcept(is_nothrow_move_constructible<_Tp>())
364 if (__other._M_engaged)
365 this->_M_construct(std::move(__other._M_get()));
369 operator=(const _Optional_base& __other)
371 if (this->_M_engaged && __other._M_engaged)
372 this->_M_get() = __other._M_get();
375 if (__other._M_engaged)
376 this->_M_construct(__other._M_get());
384 operator=(_Optional_base&& __other)
385 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
386 is_nothrow_move_assignable<_Tp>>())
388 if (this->_M_engaged && __other._M_engaged)
389 this->_M_get() = std::move(__other._M_get());
392 if (__other._M_engaged)
393 this->_M_construct(std::move(__other._M_get()));
401 // ~_Optional_base() noexcept = default;
404 constexpr bool _M_is_engaged() const noexcept
405 { return this->_M_engaged; }
409 { return _M_payload; }
412 _M_get() const noexcept
413 { return _M_payload; }
415 template<typename... _Args>
417 _M_construct(_Args&&... __args)
418 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
420 ::new (std::__addressof(this->_M_payload))
421 _Stored_type(std::forward<_Args>(__args)...);
422 this->_M_engaged = true;
428 this->_M_engaged = false;
429 this->_M_payload.~_Stored_type();
435 if (this->_M_engaged)
440 struct _Empty_byte { };
443 _Empty_byte _M_empty;
444 _Stored_type _M_payload;
446 bool _M_engaged = false;
449 template<typename _Tp>
453 struct __is_optional_impl : false_type
456 template<typename _Tp>
457 struct __is_optional_impl<optional<_Tp>> : true_type
460 template<typename _Tp>
462 : public __is_optional_impl<std::remove_cv_t<std::remove_reference_t<_Tp>>>
467 * @brief Class template for optional values.
469 template<typename _Tp>
471 : private _Optional_base<_Tp>,
472 private _Enable_copy_move<
474 is_copy_constructible<_Tp>::value,
476 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
478 is_move_constructible<_Tp>::value,
480 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
484 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
485 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
486 __not_<is_reference<_Tp>>>(),
487 "Invalid instantiation of optional<T>");
490 using _Base = _Optional_base<_Tp>;
493 using value_type = _Tp;
495 // _Optional_base has the responsibility for construction.
498 constexpr optional() = default;
499 // Converting constructors for engaged optionals.
500 template <typename _Up,
502 __not_<is_same<_Tp, _Up>>,
503 is_constructible<_Tp, _Up&&>,
504 is_convertible<_Up&&, _Tp>
505 >::value, bool> = true>
506 constexpr optional(_Up&& __t)
507 : _Base(_Tp(std::forward<_Up>(__t))) { }
509 template <typename _Up,
511 __not_<is_same<_Tp, _Up>>,
512 is_constructible<_Tp, _Up&&>,
513 __not_<is_convertible<_Up&&, _Tp>>
514 >::value, bool> = false>
515 explicit constexpr optional(_Up&& __t)
516 : _Base(_Tp(std::forward<_Up>(__t))) { }
518 template <typename _Up,
520 __not_<is_same<_Tp, _Up>>,
521 __not_<is_constructible<
522 _Tp, const optional<_Up>&>>,
523 __not_<is_convertible<
524 const optional<_Up>&, _Tp>>,
525 is_constructible<_Tp, const _Up&>,
526 is_convertible<const _Up&, _Tp>
527 >::value, bool> = true>
528 constexpr optional(const optional<_Up>& __t)
529 : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
531 template <typename _Up,
533 __not_<is_same<_Tp, _Up>>,
534 __not_<is_constructible<
535 _Tp, const optional<_Up>&>>,
536 __not_<is_convertible<
537 const optional<_Up>&, _Tp>>,
538 is_constructible<_Tp, const _Up&>,
539 __not_<is_convertible<const _Up&, _Tp>>
540 >::value, bool> = false>
541 explicit constexpr optional(const optional<_Up>& __t)
542 : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
544 template <typename _Up,
546 __not_<is_same<_Tp, _Up>>,
547 __not_<is_constructible<
548 _Tp, optional<_Up>&&>>,
549 __not_<is_convertible<
550 optional<_Up>&&, _Tp>>,
551 is_constructible<_Tp, _Up&&>,
552 is_convertible<_Up&&, _Tp>
553 >::value, bool> = true>
554 constexpr optional(optional<_Up>&& __t)
555 : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
557 template <typename _Up,
559 __not_<is_same<_Tp, _Up>>,
560 __not_<is_constructible<
561 _Tp, optional<_Up>&&>>,
562 __not_<is_convertible<
563 optional<_Up>&&, _Tp>>,
564 is_constructible<_Tp, _Up&&>,
565 __not_<is_convertible<_Up&&, _Tp>>
566 >::value, bool> = false>
567 explicit constexpr optional(optional<_Up>&& __t)
568 : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
570 // [X.Y.4.3] (partly) Assignment.
572 operator=(nullopt_t) noexcept
578 template<typename _Up,
580 __not_<is_same<_Up, nullopt_t>>,
581 __not_<__is_optional<_Up>>>::value,
586 static_assert(__and_<is_constructible<_Tp, _Up>,
587 is_assignable<_Tp&, _Up>>(),
588 "Cannot assign to value type from argument");
590 if (this->_M_is_engaged())
591 this->_M_get() = std::forward<_Up>(__u);
593 this->_M_construct(std::forward<_Up>(__u));
598 template<typename _Up,
600 __not_<is_same<_Tp, _Up>>>::value,
603 operator=(const optional<_Up>& __u)
605 static_assert(__and_<is_constructible<_Tp, _Up>,
606 is_assignable<_Tp&, _Up>>(),
607 "Cannot assign to value type from argument");
611 if (this->_M_is_engaged())
612 this->_M_get() = *__u;
614 this->_M_construct(*__u);
623 template<typename _Up,
625 __not_<is_same<_Tp, _Up>>>::value,
628 operator=(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() = std::move(*__u);
639 this->_M_construct(std::move(*__u));
649 template<typename... _Args>
651 emplace(_Args&&... __args)
653 static_assert(is_constructible<_Tp, _Args&&...>(),
654 "Cannot emplace value type from arguments");
657 this->_M_construct(std::forward<_Args>(__args)...);
660 template<typename _Up, typename... _Args>
661 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
663 emplace(initializer_list<_Up> __il, _Args&&... __args)
666 this->_M_construct(__il, std::forward<_Args>(__args)...);
669 // [X.Y.4.2] Destructor is implicit, implemented in _Optional_base.
673 swap(optional& __other)
674 noexcept(is_nothrow_move_constructible<_Tp>()
675 && noexcept(swap(declval<_Tp&>(), declval<_Tp&>())))
679 if (this->_M_is_engaged() && __other._M_is_engaged())
680 swap(this->_M_get(), __other._M_get());
681 else if (this->_M_is_engaged())
683 __other._M_construct(std::move(this->_M_get()));
686 else if (__other._M_is_engaged())
688 this->_M_construct(std::move(__other._M_get()));
689 __other._M_destruct();
693 // [X.Y.4.5] Observers.
696 { return __constexpr_addressof(this->_M_get()); }
700 { return std::__addressof(this->_M_get()); }
704 { return this->_M_get(); }
708 { return this->_M_get(); }
712 { return std::move(this->_M_get()); }
714 constexpr const _Tp&&
716 { return std::move(this->_M_get()); }
718 constexpr explicit operator bool() const noexcept
719 { return this->_M_is_engaged(); }
724 return this->_M_is_engaged()
726 : (__throw_bad_optional_access("Attempt to access value of a "
727 "disengaged optional object"),
734 return this->_M_is_engaged()
736 : (__throw_bad_optional_access("Attempt to access value of a "
737 "disengaged optional object"),
744 return this->_M_is_engaged()
745 ? std::move(this->_M_get())
746 : (__throw_bad_optional_access("Attempt to access value of a "
747 "disengaged optional object"),
748 std::move(this->_M_get()));
751 constexpr const _Tp&&
754 return this->_M_is_engaged()
755 ? std::move(this->_M_get())
756 : (__throw_bad_optional_access("Attempt to access value of a "
757 "disengaged optional object"),
758 std::move(this->_M_get()));
761 template<typename _Up>
763 value_or(_Up&& __u) const&
765 static_assert(__and_<is_copy_constructible<_Tp>,
766 is_convertible<_Up&&, _Tp>>(),
767 "Cannot return value");
769 return this->_M_is_engaged()
771 : static_cast<_Tp>(std::forward<_Up>(__u));
774 template<typename _Up>
776 value_or(_Up&& __u) &&
778 static_assert(__and_<is_move_constructible<_Tp>,
779 is_convertible<_Up&&, _Tp>>(),
780 "Cannot return value" );
782 return this->_M_is_engaged()
783 ? std::move(this->_M_get())
784 : static_cast<_Tp>(std::forward<_Up>(__u));
788 // [X.Y.8] Comparisons between optional values.
789 template<typename _Tp>
791 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
793 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
794 && (!__lhs || *__lhs == *__rhs);
797 template<typename _Tp>
799 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
800 { return !(__lhs == __rhs); }
802 template<typename _Tp>
804 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
806 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
809 template<typename _Tp>
811 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
812 { return __rhs < __lhs; }
814 template<typename _Tp>
816 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
817 { return !(__rhs < __lhs); }
819 template<typename _Tp>
821 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
822 { return !(__lhs < __rhs); }
824 // [X.Y.9] Comparisons with nullopt.
825 template<typename _Tp>
827 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
830 template<typename _Tp>
832 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
835 template<typename _Tp>
837 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
838 { return static_cast<bool>(__lhs); }
840 template<typename _Tp>
842 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
843 { return static_cast<bool>(__rhs); }
845 template<typename _Tp>
847 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
850 template<typename _Tp>
852 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
853 { return static_cast<bool>(__rhs); }
855 template<typename _Tp>
857 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
858 { return static_cast<bool>(__lhs); }
860 template<typename _Tp>
862 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
865 template<typename _Tp>
867 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
870 template<typename _Tp>
872 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
875 template<typename _Tp>
877 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
880 template<typename _Tp>
882 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
885 // [X.Y.10] Comparisons with value type.
886 template<typename _Tp>
888 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
889 { return __lhs && *__lhs == __rhs; }
891 template<typename _Tp>
893 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
894 { return __rhs && __lhs == *__rhs; }
896 template<typename _Tp>
898 operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
899 { return !__lhs || !(*__lhs == __rhs); }
901 template<typename _Tp>
903 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
904 { return !__rhs || !(__lhs == *__rhs); }
906 template<typename _Tp>
908 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
909 { return !__lhs || *__lhs < __rhs; }
911 template<typename _Tp>
913 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
914 { return __rhs && __lhs < *__rhs; }
916 template<typename _Tp>
918 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
919 { return __lhs && __rhs < *__lhs; }
921 template<typename _Tp>
923 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
924 { return !__rhs || *__rhs < __lhs; }
926 template<typename _Tp>
928 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
929 { return !__lhs || !(__rhs < *__lhs); }
931 template<typename _Tp>
933 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
934 { return __rhs && !(*__rhs < __lhs); }
936 template<typename _Tp>
938 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
939 { return __lhs && !(*__lhs < __rhs); }
941 template<typename _Tp>
943 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
944 { return !__rhs || !(__lhs < *__rhs); }
947 template<typename _Tp>
949 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
950 noexcept(noexcept(__lhs.swap(__rhs)))
951 { __lhs.swap(__rhs); }
953 template<typename _Tp>
954 constexpr optional<decay_t<_Tp>>
955 make_optional(_Tp&& __t)
956 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
959 template<typename _Tp>
960 struct hash<optional<_Tp>>
962 using result_type = size_t;
963 using argument_type = optional<_Tp>;
966 operator()(const optional<_Tp>& __t) const
967 noexcept(noexcept(hash<_Tp> {}(*__t)))
969 // We pick an arbitrary hash for disengaged optionals which hopefully
970 // usual values of _Tp won't typically hash to.
971 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
972 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
978 _GLIBCXX_END_NAMESPACE_VERSION
983 #endif // _GLIBCXX_OPTIONAL