1// <experimental/optional> -*- C++ -*-
3// Copyright (C) 2013-2022 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.
30#ifndef _GLIBCXX_EXPERIMENTAL_OPTIONAL
31#define _GLIBCXX_EXPERIMENTAL_OPTIONAL 1
33#if __cplusplus >= 201402L
38#include <initializer_list>
39#include <bits/functexcept.h>
40#include <bits/functional_hash.h>
41#include <bits/enable_special_members.h>
43#include <experimental/bits/lfts_config.h>
45namespace std _GLIBCXX_VISIBILITY(default)
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
51inline namespace fundamentals_v1
54 * @defgroup optional Optional values
57 * Class template for optional values and surrounding facilities, as
58 * described in n3793 "A proposal to add a utility class to represent
59 * optional objects (Revision 5)".
64#define __cpp_lib_experimental_optional 201411
66 // All subsequent [X.Y.n] references are against n3793.
69 template<typename _Tp>
73 /// Tag type for in-place construction.
74 struct in_place_t { };
76 /// Tag for in-place construction.
77 constexpr in_place_t in_place { };
80 /// Tag type to disengage optional objects.
83 // Do not user-declare default constructor at all for
84 // optional_value = {} syntax to work.
85 // nullopt_t() = delete;
87 // Used for constructing nullopt.
88 enum class _Construct { _Token };
90 // Must be constexpr for nullopt_t to be literal.
91 explicit constexpr nullopt_t(_Construct) { }
95 /// Tag to disengage optional objects.
96 constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
100 * @brief Exception class thrown when a disengaged optional object is
102 * @ingroup exceptions
104 class bad_optional_access : public logic_error
107 bad_optional_access() : logic_error("bad optional access") { }
109 // XXX This constructor is non-standard. Should not be inline
110 explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
112 virtual ~bad_optional_access() noexcept = default;
115 /// @cond undocumented
117 // XXX Does not belong here.
118 [[noreturn]] inline void
119 __throw_bad_optional_access(const char* __s)
120 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
123 * @brief Class template that holds the necessary state for @ref optional
124 * and that has the responsibility for construction and the special members.
126 * Such a separate base class template is necessary in order to
127 * conditionally enable the special members (e.g. copy/move constructors).
128 * Note that this means that @ref _Optional_base implements the
129 * functionality for copy and move assignment, but not for converting
132 * @see optional, _Enable_special_members
134 template<typename _Tp, bool _ShouldProvideDestructor =
135 !is_trivially_destructible<_Tp>::value>
139 // Remove const to avoid prohibition of reusing object storage for
140 // const-qualified types in [3.8/9]. This is strictly internal
141 // and even optional itself is oblivious to it.
142 using _Stored_type = remove_const_t<_Tp>;
145 // [X.Y.4.1] Constructors.
147 // Constructors for disengaged optionals.
148 constexpr _Optional_base() noexcept
151 constexpr _Optional_base(nullopt_t) noexcept
152 : _Optional_base{} { }
154 // Constructors for engaged optionals.
155 template<typename... _Args>
156 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
157 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
159 template<typename _Up, typename... _Args,
160 enable_if_t<is_constructible<_Tp,
161 initializer_list<_Up>&,
164 constexpr explicit _Optional_base(in_place_t,
165 initializer_list<_Up> __il,
167 : _M_payload(__il, std::forward<_Args>(__args)...),
170 // Copy and move constructors.
171 _Optional_base(const _Optional_base& __other)
173 if (__other._M_engaged)
174 this->_M_construct(__other._M_get());
177 _Optional_base(_Optional_base&& __other)
178 noexcept(is_nothrow_move_constructible<_Tp>())
180 if (__other._M_engaged)
181 this->_M_construct(std::move(__other._M_get()));
184 // [X.Y.4.3] (partly) Assignment.
186 operator=(const _Optional_base& __other)
188 if (this->_M_engaged && __other._M_engaged)
189 this->_M_get() = __other._M_get();
192 if (__other._M_engaged)
193 this->_M_construct(__other._M_get());
202 operator=(_Optional_base&& __other)
203 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
204 is_nothrow_move_assignable<_Tp>>())
206 if (this->_M_engaged && __other._M_engaged)
207 this->_M_get() = std::move(__other._M_get());
210 if (__other._M_engaged)
211 this->_M_construct(std::move(__other._M_get()));
218 // [X.Y.4.2] Destructor.
221 if (this->_M_engaged)
222 this->_M_payload.~_Stored_type();
225 // The following functionality is also needed by optional, hence the
226 // protected accessibility.
228 constexpr bool _M_is_engaged() const noexcept
229 { return this->_M_engaged; }
231 // The _M_get operations have _M_engaged as a precondition.
234 { return _M_payload; }
237 _M_get() const noexcept
238 { return _M_payload; }
240 // The _M_construct operation has !_M_engaged as a precondition
241 // while _M_destruct has _M_engaged as a precondition.
242 template<typename... _Args>
244 _M_construct(_Args&&... __args)
245 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
247 ::new (std::__addressof(this->_M_payload))
248 _Stored_type(std::forward<_Args>(__args)...);
249 this->_M_engaged = true;
255 this->_M_engaged = false;
256 this->_M_payload.~_Stored_type();
259 // _M_reset is a 'safe' operation with no precondition.
263 if (this->_M_engaged)
268 struct _Empty_byte { };
270 _Empty_byte _M_empty;
271 _Stored_type _M_payload;
273 bool _M_engaged = false;
276 /// Partial specialization that is exactly identical to the primary template
277 /// save for not providing a destructor, to fulfill triviality requirements.
278 template<typename _Tp>
279 class _Optional_base<_Tp, false>
282 using _Stored_type = remove_const_t<_Tp>;
285 constexpr _Optional_base() noexcept
288 constexpr _Optional_base(nullopt_t) noexcept
289 : _Optional_base{} { }
291 template<typename... _Args>
292 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
293 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
295 template<typename _Up, typename... _Args,
296 enable_if_t<is_constructible<_Tp,
297 initializer_list<_Up>&,
300 constexpr explicit _Optional_base(in_place_t,
301 initializer_list<_Up> __il,
303 : _M_payload(__il, std::forward<_Args>(__args)...),
306 _Optional_base(const _Optional_base& __other)
308 if (__other._M_engaged)
309 this->_M_construct(__other._M_get());
312 _Optional_base(_Optional_base&& __other)
313 noexcept(is_nothrow_move_constructible<_Tp>())
315 if (__other._M_engaged)
316 this->_M_construct(std::move(__other._M_get()));
320 operator=(const _Optional_base& __other)
322 if (this->_M_engaged && __other._M_engaged)
323 this->_M_get() = __other._M_get();
326 if (__other._M_engaged)
327 this->_M_construct(__other._M_get());
335 operator=(_Optional_base&& __other)
336 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
337 is_nothrow_move_assignable<_Tp>>())
339 if (this->_M_engaged && __other._M_engaged)
340 this->_M_get() = std::move(__other._M_get());
343 if (__other._M_engaged)
344 this->_M_construct(std::move(__other._M_get()));
352 // ~_Optional_base() noexcept = default;
355 constexpr bool _M_is_engaged() const noexcept
356 { return this->_M_engaged; }
360 { return _M_payload; }
363 _M_get() const noexcept
364 { return _M_payload; }
366 template<typename... _Args>
368 _M_construct(_Args&&... __args)
369 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
371 ::new (std::__addressof(this->_M_payload))
372 _Stored_type(std::forward<_Args>(__args)...);
373 this->_M_engaged = true;
379 this->_M_engaged = false;
380 this->_M_payload.~_Stored_type();
386 if (this->_M_engaged)
391 struct _Empty_byte { };
394 _Empty_byte _M_empty;
395 _Stored_type _M_payload;
397 bool _M_engaged = false;
400 template<typename _Tp, typename _Up>
401 using __converts_from_optional =
402 __or_<is_constructible<_Tp, const optional<_Up>&>,
403 is_constructible<_Tp, optional<_Up>&>,
404 is_constructible<_Tp, const optional<_Up>&&>,
405 is_constructible<_Tp, optional<_Up>&&>,
406 is_convertible<const optional<_Up>&, _Tp>,
407 is_convertible<optional<_Up>&, _Tp>,
408 is_convertible<const optional<_Up>&&, _Tp>,
409 is_convertible<optional<_Up>&&, _Tp>>;
411 template<typename _Tp, typename _Up>
412 using __assigns_from_optional =
413 __or_<is_assignable<_Tp&, const optional<_Up>&>,
414 is_assignable<_Tp&, optional<_Up>&>,
415 is_assignable<_Tp&, const optional<_Up>&&>,
416 is_assignable<_Tp&, optional<_Up>&&>>;
421 * @brief Class template for optional values.
423 template<typename _Tp>
425 : private _Optional_base<_Tp>,
426 private _Enable_copy_move<
428 is_copy_constructible<_Tp>::value,
430 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
432 is_move_constructible<_Tp>::value,
434 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
438 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
439 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
440 __not_<is_reference<_Tp>>>(),
441 "Invalid instantiation of optional<T>");
444 using _Base = _Optional_base<_Tp>;
447 using value_type = _Tp;
449 // _Optional_base has the responsibility for construction.
452 constexpr optional() = default;
453 // Converting constructors for engaged optionals.
454 template <typename _Up = _Tp,
456 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
457 is_constructible<_Tp, _Up&&>,
458 is_convertible<_Up&&, _Tp>
459 >::value, bool> = true>
460 constexpr optional(_Up&& __t)
461 : _Base(in_place, std::forward<_Up>(__t)) { }
463 template <typename _Up = _Tp,
465 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
466 is_constructible<_Tp, _Up&&>,
467 __not_<is_convertible<_Up&&, _Tp>>
468 >::value, bool> = false>
469 explicit constexpr optional(_Up&& __t)
470 : _Base(in_place, std::forward<_Up>(__t)) { }
472 template <typename _Up,
474 __not_<is_same<_Tp, _Up>>,
475 is_constructible<_Tp, const _Up&>,
476 is_convertible<const _Up&, _Tp>,
477 __not_<__converts_from_optional<_Tp, _Up>>
478 >::value, bool> = true>
479 constexpr optional(const optional<_Up>& __t)
485 template <typename _Up,
487 __not_<is_same<_Tp, _Up>>,
488 is_constructible<_Tp, const _Up&>,
489 __not_<is_convertible<const _Up&, _Tp>>,
490 __not_<__converts_from_optional<_Tp, _Up>>
491 >::value, bool> = false>
492 explicit constexpr optional(const optional<_Up>& __t)
498 template <typename _Up,
500 __not_<is_same<_Tp, _Up>>,
501 is_constructible<_Tp, _Up&&>,
502 is_convertible<_Up&&, _Tp>,
503 __not_<__converts_from_optional<_Tp, _Up>>
504 >::value, bool> = true>
505 constexpr optional(optional<_Up>&& __t)
508 emplace(std::move(*__t));
511 template <typename _Up,
513 __not_<is_same<_Tp, _Up>>,
514 is_constructible<_Tp, _Up&&>,
515 __not_<is_convertible<_Up&&, _Tp>>,
516 __not_<__converts_from_optional<_Tp, _Up>>
517 >::value, bool> = false>
518 explicit constexpr optional(optional<_Up>&& __t)
521 emplace(std::move(*__t));
524 // [X.Y.4.3] (partly) Assignment.
526 operator=(nullopt_t) noexcept
532 template<typename _Up = _Tp>
534 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
535 is_constructible<_Tp, _Up>,
536 __not_<__and_<is_scalar<_Tp>,
537 is_same<_Tp, decay_t<_Up>>>>,
538 is_assignable<_Tp&, _Up>>::value,
542 if (this->_M_is_engaged())
543 this->_M_get() = std::forward<_Up>(__u);
545 this->_M_construct(std::forward<_Up>(__u));
550 template<typename _Up>
552 __not_<is_same<_Tp, _Up>>,
553 is_constructible<_Tp, const _Up&>,
554 is_assignable<_Tp&, _Up>,
555 __not_<__converts_from_optional<_Tp, _Up>>,
556 __not_<__assigns_from_optional<_Tp, _Up>>
559 operator=(const optional<_Up>& __u)
563 if (this->_M_is_engaged())
564 this->_M_get() = *__u;
566 this->_M_construct(*__u);
575 template<typename _Up>
577 __not_<is_same<_Tp, _Up>>,
578 is_constructible<_Tp, _Up>,
579 is_assignable<_Tp&, _Up>,
580 __not_<__converts_from_optional<_Tp, _Up>>,
581 __not_<__assigns_from_optional<_Tp, _Up>>
584 operator=(optional<_Up>&& __u)
588 if (this->_M_is_engaged())
589 this->_M_get() = std::move(*__u);
591 this->_M_construct(std::move(*__u));
601 template<typename... _Args>
602 enable_if_t<is_constructible<_Tp, _Args&&...>::value>
603 emplace(_Args&&... __args)
606 this->_M_construct(std::forward<_Args>(__args)...);
609 template<typename _Up, typename... _Args>
610 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
612 emplace(initializer_list<_Up> __il, _Args&&... __args)
615 this->_M_construct(__il, std::forward<_Args>(__args)...);
618 // [X.Y.4.2] Destructor is implicit, implemented in _Optional_base.
622 swap(optional& __other)
623 noexcept(is_nothrow_move_constructible<_Tp>()
624 && __is_nothrow_swappable<_Tp>::value)
628 if (this->_M_is_engaged() && __other._M_is_engaged())
629 swap(this->_M_get(), __other._M_get());
630 else if (this->_M_is_engaged())
632 __other._M_construct(std::move(this->_M_get()));
635 else if (__other._M_is_engaged())
637 this->_M_construct(std::move(__other._M_get()));
638 __other._M_destruct();
642 // [X.Y.4.5] Observers.
645 { return std::__addressof(this->_M_get()); }
649 { return std::__addressof(this->_M_get()); }
653 { return this->_M_get(); }
657 { return this->_M_get(); }
661 { return std::move(this->_M_get()); }
663 constexpr const _Tp&&
665 { return std::move(this->_M_get()); }
667 constexpr explicit operator bool() const noexcept
668 { return this->_M_is_engaged(); }
673 if (this->_M_is_engaged())
674 return this->_M_get();
675 __throw_bad_optional_access("Attempt to access value of a "
676 "disengaged optional object");
682 if (this->_M_is_engaged())
683 return this->_M_get();
684 __throw_bad_optional_access("Attempt to access value of a "
685 "disengaged optional object");
691 if (this->_M_is_engaged())
692 return std::move(this->_M_get());
693 __throw_bad_optional_access("Attempt to access value of a "
694 "disengaged optional object");
697 constexpr const _Tp&&
700 if (this->_M_is_engaged())
701 return std::move(this->_M_get());
702 __throw_bad_optional_access("Attempt to access value of a "
703 "disengaged optional object");
706 template<typename _Up>
708 value_or(_Up&& __u) const&
710 static_assert(__and_<is_copy_constructible<_Tp>,
711 is_convertible<_Up&&, _Tp>>(),
712 "Cannot return value");
714 if (this->_M_is_engaged())
715 return this->_M_get();
717 return static_cast<_Tp>(std::forward<_Up>(__u));
720 template<typename _Up>
722 value_or(_Up&& __u) &&
724 static_assert(__and_<is_move_constructible<_Tp>,
725 is_convertible<_Up&&, _Tp>>(),
726 "Cannot return value" );
728 if (this->_M_is_engaged())
729 return std::move(this->_M_get());
731 return static_cast<_Tp>(std::forward<_Up>(__u));
735 /// @relates experimental::optional @{
737 // [X.Y.8] Comparisons between optional values.
738 template<typename _Tp>
740 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
742 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
743 && (!__lhs || *__lhs == *__rhs);
746 template<typename _Tp>
748 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
749 { return !(__lhs == __rhs); }
751 template<typename _Tp>
753 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
755 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
758 template<typename _Tp>
760 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
761 { return __rhs < __lhs; }
763 template<typename _Tp>
765 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
766 { return !(__rhs < __lhs); }
768 template<typename _Tp>
770 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
771 { return !(__lhs < __rhs); }
773 // [X.Y.9] Comparisons with nullopt.
774 template<typename _Tp>
776 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
779 template<typename _Tp>
781 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
784 template<typename _Tp>
786 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
787 { return static_cast<bool>(__lhs); }
789 template<typename _Tp>
791 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
792 { return static_cast<bool>(__rhs); }
794 template<typename _Tp>
796 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
799 template<typename _Tp>
801 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
802 { return static_cast<bool>(__rhs); }
804 template<typename _Tp>
806 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
807 { return static_cast<bool>(__lhs); }
809 template<typename _Tp>
811 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
814 template<typename _Tp>
816 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
819 template<typename _Tp>
821 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
824 template<typename _Tp>
826 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
829 template<typename _Tp>
831 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
834 // [X.Y.10] Comparisons with value type.
835 template<typename _Tp>
837 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
838 { return __lhs && *__lhs == __rhs; }
840 template<typename _Tp>
842 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
843 { return __rhs && __lhs == *__rhs; }
845 template<typename _Tp>
847 operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
848 { return !__lhs || !(*__lhs == __rhs); }
850 template<typename _Tp>
852 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
853 { return !__rhs || !(__lhs == *__rhs); }
855 template<typename _Tp>
857 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
858 { return !__lhs || *__lhs < __rhs; }
860 template<typename _Tp>
862 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
863 { return __rhs && __lhs < *__rhs; }
865 template<typename _Tp>
867 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
868 { return __lhs && __rhs < *__lhs; }
870 template<typename _Tp>
872 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
873 { return !__rhs || *__rhs < __lhs; }
875 template<typename _Tp>
877 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
878 { return !__lhs || !(__rhs < *__lhs); }
880 template<typename _Tp>
882 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
883 { return __rhs && !(*__rhs < __lhs); }
885 template<typename _Tp>
887 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
888 { return __lhs && !(*__lhs < __rhs); }
890 template<typename _Tp>
892 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
893 { return !__rhs || !(__lhs < *__rhs); }
896 template<typename _Tp>
898 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
899 noexcept(noexcept(__lhs.swap(__rhs)))
900 { __lhs.swap(__rhs); }
902 template<typename _Tp>
903 constexpr optional<decay_t<_Tp>>
904 make_optional(_Tp&& __t)
905 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
907 /// @} relates experimental::optional
908 /// @} group optional
909} // namespace fundamentals_v1
910} // namespace experimental
913 /// std::hash partial specialization for experimental::optional
914 /// @relates experimental::optional
915 template<typename _Tp>
916 struct hash<experimental::optional<_Tp>>
918 using result_type = size_t;
919 using argument_type = experimental::optional<_Tp>;
922 operator()(const experimental::optional<_Tp>& __t) const
923 noexcept(noexcept(hash<_Tp> {}(*__t)))
925 // We pick an arbitrary hash for disengaged optionals which hopefully
926 // usual values of _Tp won't typically hash to.
927 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
928 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
932_GLIBCXX_END_NAMESPACE_VERSION
937#endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL