1// <experimental/optional> -*- C++ -*-
3// Copyright (C) 2013-2023 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#include <bits/requires_hosted.h> // experimental is currently omitted
35#if __cplusplus >= 201402L
40#include <initializer_list>
41#include <bits/functexcept.h>
42#include <bits/functional_hash.h>
43#include <bits/enable_special_members.h>
45#include <experimental/bits/lfts_config.h>
47namespace std _GLIBCXX_VISIBILITY(default)
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
53inline namespace fundamentals_v1
56 * @defgroup optional Optional values
59 * Class template for optional values and surrounding facilities, as
60 * described in n3793 "A proposal to add a utility class to represent
61 * optional objects (Revision 5)".
66#define __cpp_lib_experimental_optional 201411
68 // All subsequent [X.Y.n] references are against n3793.
71 template<typename _Tp>
75 /// Tag type for in-place construction.
76 struct in_place_t { };
78 /// Tag for in-place construction.
79 constexpr in_place_t in_place { };
82 /// Tag type to disengage optional objects.
85 // Do not user-declare default constructor at all for
86 // optional_value = {} syntax to work.
87 // nullopt_t() = delete;
89 // Used for constructing nullopt.
90 enum class _Construct { _Token };
92 // Must be constexpr for nullopt_t to be literal.
93 explicit constexpr nullopt_t(_Construct) { }
97 /// Tag to disengage optional objects.
98 constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
102 * @brief Exception class thrown when a disengaged optional object is
104 * @ingroup exceptions
106 class bad_optional_access : public logic_error
109 bad_optional_access() : logic_error("bad optional access") { }
111 // XXX This constructor is non-standard. Should not be inline
112 explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
114 virtual ~bad_optional_access() noexcept = default;
117 /// @cond undocumented
119 // XXX Does not belong here.
120 [[noreturn]] inline void
121 __throw_bad_optional_access(const char* __s)
122 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
125 * @brief Class template that holds the necessary state for @ref optional
126 * and that has the responsibility for construction and the special members.
128 * Such a separate base class template is necessary in order to
129 * conditionally enable the special members (e.g. copy/move constructors).
130 * Note that this means that @ref _Optional_base implements the
131 * functionality for copy and move assignment, but not for converting
134 * @see optional, _Enable_special_members
136 template<typename _Tp, bool _ShouldProvideDestructor =
137 !is_trivially_destructible<_Tp>::value>
141 // Remove const to avoid prohibition of reusing object storage for
142 // const-qualified types in [3.8/9]. This is strictly internal
143 // and even optional itself is oblivious to it.
144 using _Stored_type = remove_const_t<_Tp>;
147 // [X.Y.4.1] Constructors.
149 // Constructors for disengaged optionals.
150 constexpr _Optional_base() noexcept
153 constexpr _Optional_base(nullopt_t) noexcept
154 : _Optional_base{} { }
156 // Constructors for engaged optionals.
157 template<typename... _Args>
158 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
159 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
161 template<typename _Up, typename... _Args,
162 enable_if_t<is_constructible<_Tp,
163 initializer_list<_Up>&,
166 constexpr explicit _Optional_base(in_place_t,
167 initializer_list<_Up> __il,
169 : _M_payload(__il, std::forward<_Args>(__args)...),
172 // Copy and move constructors.
173 _Optional_base(const _Optional_base& __other)
175 if (__other._M_engaged)
176 this->_M_construct(__other._M_get());
179 _Optional_base(_Optional_base&& __other)
180 noexcept(is_nothrow_move_constructible<_Tp>())
182 if (__other._M_engaged)
183 this->_M_construct(std::move(__other._M_get()));
186 // [X.Y.4.3] (partly) Assignment.
188 operator=(const _Optional_base& __other)
190 if (this->_M_engaged && __other._M_engaged)
191 this->_M_get() = __other._M_get();
194 if (__other._M_engaged)
195 this->_M_construct(__other._M_get());
204 operator=(_Optional_base&& __other)
205 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
206 is_nothrow_move_assignable<_Tp>>())
208 if (this->_M_engaged && __other._M_engaged)
209 this->_M_get() = std::move(__other._M_get());
212 if (__other._M_engaged)
213 this->_M_construct(std::move(__other._M_get()));
220 // [X.Y.4.2] Destructor.
223 if (this->_M_engaged)
224 this->_M_payload.~_Stored_type();
227 // The following functionality is also needed by optional, hence the
228 // protected accessibility.
230 constexpr bool _M_is_engaged() const noexcept
231 { return this->_M_engaged; }
233 // The _M_get operations have _M_engaged as a precondition.
236 { return _M_payload; }
239 _M_get() const noexcept
240 { return _M_payload; }
242 // The _M_construct operation has !_M_engaged as a precondition
243 // while _M_destruct has _M_engaged as a precondition.
244 template<typename... _Args>
246 _M_construct(_Args&&... __args)
247 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
249 ::new (std::__addressof(this->_M_payload))
250 _Stored_type(std::forward<_Args>(__args)...);
251 this->_M_engaged = true;
257 this->_M_engaged = false;
258 this->_M_payload.~_Stored_type();
261 // _M_reset is a 'safe' operation with no precondition.
265 if (this->_M_engaged)
270 struct _Empty_byte { };
272 _Empty_byte _M_empty;
273 _Stored_type _M_payload;
275 bool _M_engaged = false;
278 /// Partial specialization that is exactly identical to the primary template
279 /// save for not providing a destructor, to fulfill triviality requirements.
280 template<typename _Tp>
281 class _Optional_base<_Tp, false>
284 using _Stored_type = remove_const_t<_Tp>;
287 constexpr _Optional_base() noexcept
290 constexpr _Optional_base(nullopt_t) noexcept
291 : _Optional_base{} { }
293 template<typename... _Args>
294 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
295 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
297 template<typename _Up, typename... _Args,
298 enable_if_t<is_constructible<_Tp,
299 initializer_list<_Up>&,
302 constexpr explicit _Optional_base(in_place_t,
303 initializer_list<_Up> __il,
305 : _M_payload(__il, std::forward<_Args>(__args)...),
308 _Optional_base(const _Optional_base& __other)
310 if (__other._M_engaged)
311 this->_M_construct(__other._M_get());
314 _Optional_base(_Optional_base&& __other)
315 noexcept(is_nothrow_move_constructible<_Tp>())
317 if (__other._M_engaged)
318 this->_M_construct(std::move(__other._M_get()));
322 operator=(const _Optional_base& __other)
324 if (this->_M_engaged && __other._M_engaged)
325 this->_M_get() = __other._M_get();
328 if (__other._M_engaged)
329 this->_M_construct(__other._M_get());
337 operator=(_Optional_base&& __other)
338 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
339 is_nothrow_move_assignable<_Tp>>())
341 if (this->_M_engaged && __other._M_engaged)
342 this->_M_get() = std::move(__other._M_get());
345 if (__other._M_engaged)
346 this->_M_construct(std::move(__other._M_get()));
354 // ~_Optional_base() noexcept = default;
357 constexpr bool _M_is_engaged() const noexcept
358 { return this->_M_engaged; }
362 { return _M_payload; }
365 _M_get() const noexcept
366 { return _M_payload; }
368 template<typename... _Args>
370 _M_construct(_Args&&... __args)
371 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
373 ::new (std::__addressof(this->_M_payload))
374 _Stored_type(std::forward<_Args>(__args)...);
375 this->_M_engaged = true;
381 this->_M_engaged = false;
382 this->_M_payload.~_Stored_type();
388 if (this->_M_engaged)
393 struct _Empty_byte { };
396 _Empty_byte _M_empty;
397 _Stored_type _M_payload;
399 bool _M_engaged = false;
402 template<typename _Tp, typename _Up>
403 using __converts_from_optional =
404 __or_<is_constructible<_Tp, const optional<_Up>&>,
405 is_constructible<_Tp, optional<_Up>&>,
406 is_constructible<_Tp, const optional<_Up>&&>,
407 is_constructible<_Tp, optional<_Up>&&>,
408 is_convertible<const optional<_Up>&, _Tp>,
409 is_convertible<optional<_Up>&, _Tp>,
410 is_convertible<const optional<_Up>&&, _Tp>,
411 is_convertible<optional<_Up>&&, _Tp>>;
413 template<typename _Tp, typename _Up>
414 using __assigns_from_optional =
415 __or_<is_assignable<_Tp&, const optional<_Up>&>,
416 is_assignable<_Tp&, optional<_Up>&>,
417 is_assignable<_Tp&, const optional<_Up>&&>,
418 is_assignable<_Tp&, optional<_Up>&&>>;
423 * @brief Class template for optional values.
425 template<typename _Tp>
427 : private _Optional_base<_Tp>,
428 private _Enable_copy_move<
430 is_copy_constructible<_Tp>::value,
432 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
434 is_move_constructible<_Tp>::value,
436 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
440 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
441 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
442 __not_<is_reference<_Tp>>>(),
443 "Invalid instantiation of optional<T>");
446 using _Base = _Optional_base<_Tp>;
449 using value_type = _Tp;
451 // _Optional_base has the responsibility for construction.
454 constexpr optional() = default;
455 // Converting constructors for engaged optionals.
456 template <typename _Up = _Tp,
458 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
459 is_constructible<_Tp, _Up&&>,
460 is_convertible<_Up&&, _Tp>
461 >::value, bool> = true>
462 constexpr optional(_Up&& __t)
463 : _Base(in_place, std::forward<_Up>(__t)) { }
465 template <typename _Up = _Tp,
467 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
468 is_constructible<_Tp, _Up&&>,
469 __not_<is_convertible<_Up&&, _Tp>>
470 >::value, bool> = false>
471 explicit constexpr optional(_Up&& __t)
472 : _Base(in_place, std::forward<_Up>(__t)) { }
474 template <typename _Up,
476 __not_<is_same<_Tp, _Up>>,
477 is_constructible<_Tp, const _Up&>,
478 is_convertible<const _Up&, _Tp>,
479 __not_<__converts_from_optional<_Tp, _Up>>
480 >::value, bool> = true>
481 constexpr optional(const optional<_Up>& __t)
487 template <typename _Up,
489 __not_<is_same<_Tp, _Up>>,
490 is_constructible<_Tp, const _Up&>,
491 __not_<is_convertible<const _Up&, _Tp>>,
492 __not_<__converts_from_optional<_Tp, _Up>>
493 >::value, bool> = false>
494 explicit constexpr optional(const optional<_Up>& __t)
500 template <typename _Up,
502 __not_<is_same<_Tp, _Up>>,
503 is_constructible<_Tp, _Up&&>,
504 is_convertible<_Up&&, _Tp>,
505 __not_<__converts_from_optional<_Tp, _Up>>
506 >::value, bool> = true>
507 constexpr optional(optional<_Up>&& __t)
510 emplace(std::move(*__t));
513 template <typename _Up,
515 __not_<is_same<_Tp, _Up>>,
516 is_constructible<_Tp, _Up&&>,
517 __not_<is_convertible<_Up&&, _Tp>>,
518 __not_<__converts_from_optional<_Tp, _Up>>
519 >::value, bool> = false>
520 explicit constexpr optional(optional<_Up>&& __t)
523 emplace(std::move(*__t));
526 // [X.Y.4.3] (partly) Assignment.
528 operator=(nullopt_t) noexcept
534 template<typename _Up = _Tp>
536 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
537 is_constructible<_Tp, _Up>,
538 __not_<__and_<is_scalar<_Tp>,
539 is_same<_Tp, decay_t<_Up>>>>,
540 is_assignable<_Tp&, _Up>>::value,
544 if (this->_M_is_engaged())
545 this->_M_get() = std::forward<_Up>(__u);
547 this->_M_construct(std::forward<_Up>(__u));
552 template<typename _Up>
554 __not_<is_same<_Tp, _Up>>,
555 is_constructible<_Tp, const _Up&>,
556 is_assignable<_Tp&, _Up>,
557 __not_<__converts_from_optional<_Tp, _Up>>,
558 __not_<__assigns_from_optional<_Tp, _Up>>
561 operator=(const optional<_Up>& __u)
565 if (this->_M_is_engaged())
566 this->_M_get() = *__u;
568 this->_M_construct(*__u);
577 template<typename _Up>
579 __not_<is_same<_Tp, _Up>>,
580 is_constructible<_Tp, _Up>,
581 is_assignable<_Tp&, _Up>,
582 __not_<__converts_from_optional<_Tp, _Up>>,
583 __not_<__assigns_from_optional<_Tp, _Up>>
586 operator=(optional<_Up>&& __u)
590 if (this->_M_is_engaged())
591 this->_M_get() = std::move(*__u);
593 this->_M_construct(std::move(*__u));
603 template<typename... _Args>
604 enable_if_t<is_constructible<_Tp, _Args&&...>::value>
605 emplace(_Args&&... __args)
608 this->_M_construct(std::forward<_Args>(__args)...);
611 template<typename _Up, typename... _Args>
612 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
614 emplace(initializer_list<_Up> __il, _Args&&... __args)
617 this->_M_construct(__il, std::forward<_Args>(__args)...);
620 // [X.Y.4.2] Destructor is implicit, implemented in _Optional_base.
624 swap(optional& __other)
625 noexcept(is_nothrow_move_constructible<_Tp>()
626 && __is_nothrow_swappable<_Tp>::value)
630 if (this->_M_is_engaged() && __other._M_is_engaged())
631 swap(this->_M_get(), __other._M_get());
632 else if (this->_M_is_engaged())
634 __other._M_construct(std::move(this->_M_get()));
637 else if (__other._M_is_engaged())
639 this->_M_construct(std::move(__other._M_get()));
640 __other._M_destruct();
644 // [X.Y.4.5] Observers.
647 { return std::__addressof(this->_M_get()); }
651 { return std::__addressof(this->_M_get()); }
655 { return this->_M_get(); }
659 { return this->_M_get(); }
663 { return std::move(this->_M_get()); }
665 constexpr const _Tp&&
667 { return std::move(this->_M_get()); }
669 constexpr explicit operator bool() const noexcept
670 { return this->_M_is_engaged(); }
675 if (this->_M_is_engaged())
676 return this->_M_get();
677 __throw_bad_optional_access("Attempt to access value of a "
678 "disengaged optional object");
684 if (this->_M_is_engaged())
685 return this->_M_get();
686 __throw_bad_optional_access("Attempt to access value of a "
687 "disengaged optional object");
693 if (this->_M_is_engaged())
694 return std::move(this->_M_get());
695 __throw_bad_optional_access("Attempt to access value of a "
696 "disengaged optional object");
699 constexpr const _Tp&&
702 if (this->_M_is_engaged())
703 return std::move(this->_M_get());
704 __throw_bad_optional_access("Attempt to access value of a "
705 "disengaged optional object");
708 template<typename _Up>
710 value_or(_Up&& __u) const&
712 static_assert(__and_<is_copy_constructible<_Tp>,
713 is_convertible<_Up&&, _Tp>>(),
714 "Cannot return value");
716 if (this->_M_is_engaged())
717 return this->_M_get();
719 return static_cast<_Tp>(std::forward<_Up>(__u));
722 template<typename _Up>
724 value_or(_Up&& __u) &&
726 static_assert(__and_<is_move_constructible<_Tp>,
727 is_convertible<_Up&&, _Tp>>(),
728 "Cannot return value" );
730 if (this->_M_is_engaged())
731 return std::move(this->_M_get());
733 return static_cast<_Tp>(std::forward<_Up>(__u));
737 /// @relates experimental::optional @{
739 // [X.Y.8] Comparisons between optional values.
740 template<typename _Tp>
742 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
744 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
745 && (!__lhs || *__lhs == *__rhs);
748 template<typename _Tp>
750 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
751 { return !(__lhs == __rhs); }
753 template<typename _Tp>
755 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
757 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
760 template<typename _Tp>
762 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
763 { return __rhs < __lhs; }
765 template<typename _Tp>
767 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
768 { return !(__rhs < __lhs); }
770 template<typename _Tp>
772 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
773 { return !(__lhs < __rhs); }
775 // [X.Y.9] Comparisons with nullopt.
776 template<typename _Tp>
778 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
781 template<typename _Tp>
783 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
786 template<typename _Tp>
788 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
789 { return static_cast<bool>(__lhs); }
791 template<typename _Tp>
793 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
794 { return static_cast<bool>(__rhs); }
796 template<typename _Tp>
798 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
801 template<typename _Tp>
803 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
804 { return static_cast<bool>(__rhs); }
806 template<typename _Tp>
808 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
809 { return static_cast<bool>(__lhs); }
811 template<typename _Tp>
813 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
816 template<typename _Tp>
818 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
821 template<typename _Tp>
823 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
826 template<typename _Tp>
828 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
831 template<typename _Tp>
833 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
836 // [X.Y.10] Comparisons with value type.
837 template<typename _Tp>
839 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
840 { return __lhs && *__lhs == __rhs; }
842 template<typename _Tp>
844 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
845 { return __rhs && __lhs == *__rhs; }
847 template<typename _Tp>
849 operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
850 { return !__lhs || !(*__lhs == __rhs); }
852 template<typename _Tp>
854 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
855 { return !__rhs || !(__lhs == *__rhs); }
857 template<typename _Tp>
859 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
860 { return !__lhs || *__lhs < __rhs; }
862 template<typename _Tp>
864 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
865 { return __rhs && __lhs < *__rhs; }
867 template<typename _Tp>
869 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
870 { return __lhs && __rhs < *__lhs; }
872 template<typename _Tp>
874 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
875 { return !__rhs || *__rhs < __lhs; }
877 template<typename _Tp>
879 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
880 { return !__lhs || !(__rhs < *__lhs); }
882 template<typename _Tp>
884 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
885 { return __rhs && !(*__rhs < __lhs); }
887 template<typename _Tp>
889 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
890 { return __lhs && !(*__lhs < __rhs); }
892 template<typename _Tp>
894 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
895 { return !__rhs || !(__lhs < *__rhs); }
898 template<typename _Tp>
900 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
901 noexcept(noexcept(__lhs.swap(__rhs)))
902 { __lhs.swap(__rhs); }
904 template<typename _Tp>
905 constexpr optional<decay_t<_Tp>>
906 make_optional(_Tp&& __t)
907 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
909 /// @} relates experimental::optional
910 /// @} group optional
911} // namespace fundamentals_v1
912} // namespace experimental
915 /// std::hash partial specialization for experimental::optional
916 /// @relates experimental::optional
917 template<typename _Tp>
918 struct hash<experimental::optional<_Tp>>
920 using result_type = size_t;
921 using argument_type = experimental::optional<_Tp>;
924 operator()(const experimental::optional<_Tp>& __t) const
925 noexcept(noexcept(hash<_Tp> {}(*__t)))
927 // We pick an arbitrary hash for disengaged optionals which hopefully
928 // usual values of _Tp won't typically hash to.
929 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
930 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
934_GLIBCXX_END_NAMESPACE_VERSION
939#endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL