2 //===-------------------------- optional ----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_OPTIONAL
12 #define _LIBCPP_OPTIONAL
20 // 23.6.3, optional for object types
21 template <class T> class optional;
23 // 23.6.4, no-value state indicator
24 struct nullopt_t{see below };
25 constexpr nullopt_t nullopt(unspecified );
27 // 23.6.5, class bad_optional_access
28 class bad_optional_access;
30 // 23.6.6, relational operators
31 template <class T, class U>
32 constexpr bool operator==(const optional<T>&, const optional<U>&);
33 template <class T, class U>
34 constexpr bool operator!=(const optional<T>&, const optional<U>&);
35 template <class T, class U>
36 constexpr bool operator<(const optional<T>&, const optional<U>&);
37 template <class T, class U>
38 constexpr bool operator>(const optional<T>&, const optional<U>&);
39 template <class T, class U>
40 constexpr bool operator<=(const optional<T>&, const optional<U>&);
41 template <class T, class U>
42 constexpr bool operator>=(const optional<T>&, const optional<U>&);
44 // 23.6.7 comparison with nullopt
45 template <class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;
46 template <class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept;
47 template <class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept;
48 template <class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept;
49 template <class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept;
50 template <class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept;
51 template <class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept;
52 template <class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept;
53 template <class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept;
54 template <class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept;
55 template <class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept;
56 template <class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept;
58 // 23.6.8, comparison with T
59 template <class T, class U> constexpr bool operator==(const optional<T>&, const U&);
60 template <class T, class U> constexpr bool operator==(const U&, const optional<T>&);
61 template <class T, class U> constexpr bool operator!=(const optional<T>&, const U&);
62 template <class T, class U> constexpr bool operator!=(const U&, const optional<T>&);
63 template <class T, class U> constexpr bool operator<(const optional<T>&, const U&);
64 template <class T, class U> constexpr bool operator<(const U&, const optional<T>&);
65 template <class T, class U> constexpr bool operator<=(const optional<T>&, const U&);
66 template <class T, class U> constexpr bool operator<=(const U&, const optional<T>&);
67 template <class T, class U> constexpr bool operator>(const optional<T>&, const U&);
68 template <class T, class U> constexpr bool operator>(const U&, const optional<T>&);
69 template <class T, class U> constexpr bool operator>=(const optional<T>&, const U&);
70 template <class T, class U> constexpr bool operator>=(const U&, const optional<T>&);
72 // 23.6.9, specialized algorithms
73 template <class T> void swap(optional<T>&, optional<T>&) noexcept(see below );
74 template <class T> constexpr optional<see below > make_optional(T&&);
75 template <class T, class... Args>
76 constexpr optional<T> make_optional(Args&&... args);
77 template <class T, class U, class... Args>
78 constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
80 // 23.6.10, hash support
81 template <class T> struct hash;
82 template <class T> struct hash<optional<T>>;
84 template <class T> class optional {
88 // 23.6.3.1, constructors
89 constexpr optional() noexcept;
90 constexpr optional(nullopt_t) noexcept;
91 optional(const optional &);
92 optional(optional &&) noexcept(see below);
93 template <class... Args> constexpr explicit optional(in_place_t, Args &&...);
94 template <class U, class... Args>
95 constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...);
96 template <class U = T>
97 constexpr EXPLICIT optional(U &&);
99 constexpr EXPLICIT optional(const optional<U> &);
101 constexpr EXPLICIT optional(optional<U> &&);
103 // 23.6.3.2, destructor
106 // 23.6.3.3, assignment
107 optional &operator=(nullopt_t) noexcept;
108 optional &operator=(const optional &);
109 optional &operator=(optional &&) noexcept(see below );
110 template <class U = T> optional &operator=(U &&);
111 template <class U> optional &operator=(const optional<U> &);
112 template <class U> optional &operator=(optional<U> &&);
113 template <class... Args> T& emplace(Args &&...);
114 template <class U, class... Args>
115 T& emplace(initializer_list<U>, Args &&...);
118 void swap(optional &) noexcept(see below );
120 // 23.6.3.5, observers
121 constexpr T const *operator->() const;
122 constexpr T *operator->();
123 constexpr T const &operator*() const &;
124 constexpr T &operator*() &;
125 constexpr T &&operator*() &&;
126 constexpr const T &&operator*() const &&;
127 constexpr explicit operator bool() const noexcept;
128 constexpr bool has_value() const noexcept;
129 constexpr T const &value() const &;
130 constexpr T &value() &;
131 constexpr T &&value() &&;
132 constexpr const T &&value() const &&;
133 template <class U> constexpr T value_or(U &&) const &;
134 template <class U> constexpr T value_or(U &&) &&;
136 // 23.6.3.6, modifiers
137 void reset() noexcept;
140 T *val; // exposition only
148 #include <__functional_base>
149 #include <__undef_min_max>
150 #include <functional>
151 #include <initializer_list>
154 #include <type_traits>
157 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
158 #pragma GCC system_header
161 namespace std // purposefully not using versioning namespace
164 class _LIBCPP_EXCEPTION_ABI bad_optional_access
168 // Get the key function ~bad_optional_access() into the dylib
169 virtual ~bad_optional_access() _NOEXCEPT;
170 virtual const char* what() const _NOEXCEPT;
175 #if _LIBCPP_STD_VER > 14
177 _LIBCPP_BEGIN_NAMESPACE_STD
180 inline _LIBCPP_INLINE_VISIBILITY
181 void __throw_bad_optional_access() {
182 #ifndef _LIBCPP_NO_EXCEPTIONS
183 throw bad_optional_access();
191 struct __secret_tag { _LIBCPP_INLINE_VISIBILITY explicit __secret_tag() = default; };
192 _LIBCPP_INLINE_VISIBILITY constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
195 /* inline */ constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
197 template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
198 struct __optional_destruct_base;
201 struct __optional_destruct_base<_Tp, false>
203 typedef _Tp value_type;
204 static_assert(is_object_v<value_type>,
205 "instantiation of optional with a non-object type is undefined behavior");
213 _LIBCPP_INLINE_VISIBILITY
214 ~__optional_destruct_base()
217 __val_.~value_type();
220 _LIBCPP_INLINE_VISIBILITY
221 constexpr __optional_destruct_base() noexcept
225 template <class... _Args>
226 _LIBCPP_INLINE_VISIBILITY
227 constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
228 : __val_(_VSTD::forward<_Args>(__args)...),
231 _LIBCPP_INLINE_VISIBILITY
232 void reset() noexcept
236 __val_.~value_type();
243 struct __optional_destruct_base<_Tp, true>
245 typedef _Tp value_type;
246 static_assert(is_object_v<value_type>,
247 "instantiation of optional with a non-object type is undefined behavior");
255 _LIBCPP_INLINE_VISIBILITY
256 constexpr __optional_destruct_base() noexcept
260 template <class... _Args>
261 _LIBCPP_INLINE_VISIBILITY
262 constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
263 : __val_(_VSTD::forward<_Args>(__args)...),
266 _LIBCPP_INLINE_VISIBILITY
267 void reset() noexcept
276 template <class _Tp, bool = is_reference<_Tp>::value>
277 struct __optional_storage_base : __optional_destruct_base<_Tp>
279 using __base = __optional_destruct_base<_Tp>;
280 using value_type = _Tp;
281 using __base::__base;
283 _LIBCPP_INLINE_VISIBILITY
284 constexpr bool has_value() const noexcept
286 return this->__engaged_;
289 _LIBCPP_INLINE_VISIBILITY
290 constexpr value_type& __get() & noexcept
294 _LIBCPP_INLINE_VISIBILITY
295 constexpr const value_type& __get() const& noexcept
299 _LIBCPP_INLINE_VISIBILITY
300 constexpr value_type&& __get() && noexcept
302 return _VSTD::move(this->__val_);
304 _LIBCPP_INLINE_VISIBILITY
305 constexpr const value_type&& __get() const&& noexcept
307 return _VSTD::move(this->__val_);
310 template <class... _Args>
311 _LIBCPP_INLINE_VISIBILITY
312 void __construct(_Args&&... __args)
314 _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage");
315 ::new((void*)_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Args>(__args)...);
316 this->__engaged_ = true;
319 template <class _That>
320 _LIBCPP_INLINE_VISIBILITY
321 void __construct_from(_That&& __opt)
323 if (__opt.has_value())
324 __construct(_VSTD::forward<_That>(__opt).__get());
327 template <class _That>
328 _LIBCPP_INLINE_VISIBILITY
329 void __assign_from(_That&& __opt)
331 if (this->__engaged_ == __opt.has_value())
333 if (this->__engaged_)
334 this->__val_ = _VSTD::forward<_That>(__opt).__get();
338 if (this->__engaged_)
341 __construct(_VSTD::forward<_That>(__opt).__get());
346 // optional<T&> is currently required ill-formed, however it may to be in the
347 // future. For this reason it has already been implemented to ensure we can
348 // make the change in an ABI compatible manner.
350 struct __optional_storage_base<_Tp, true>
352 using value_type = _Tp;
353 using __raw_type = remove_reference_t<_Tp>;
354 __raw_type* __value_;
357 static constexpr bool __can_bind_reference() {
358 using _RawUp = typename remove_reference<_Up>::type;
359 using _UpPtr = _RawUp*;
360 using _RawTp = typename remove_reference<_Tp>::type;
361 using _TpPtr = _RawTp*;
362 using _CheckLValueArg = integral_constant<bool,
363 (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value)
364 || is_same<_RawUp, reference_wrapper<_RawTp>>::value
365 || is_same<_RawUp, reference_wrapper<typename remove_const<_RawTp>::type>>::value
367 return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value)
368 || (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value &&
369 is_convertible<_UpPtr, _TpPtr>::value);
372 _LIBCPP_INLINE_VISIBILITY
373 constexpr __optional_storage_base() noexcept
374 : __value_(nullptr) {}
376 template <class _UArg>
377 _LIBCPP_INLINE_VISIBILITY
378 constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg)
379 : __value_(_VSTD::addressof(__uarg))
381 static_assert(__can_bind_reference<_UArg>(),
382 "Attempted to construct a reference element in tuple from a "
383 "possible temporary");
386 _LIBCPP_INLINE_VISIBILITY
387 void reset() noexcept { __value_ = nullptr; }
389 _LIBCPP_INLINE_VISIBILITY
390 constexpr bool has_value() const noexcept
391 { return __value_ != nullptr; }
393 _LIBCPP_INLINE_VISIBILITY
394 constexpr value_type& __get() const& noexcept
395 { return *__value_; }
397 _LIBCPP_INLINE_VISIBILITY
398 constexpr value_type&& __get() const&& noexcept
399 { return _VSTD::forward<value_type>(*__value_); }
401 template <class _UArg>
402 _LIBCPP_INLINE_VISIBILITY
403 void __construct(_UArg&& __val)
405 _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage");
406 static_assert(__can_bind_reference<_UArg>(),
407 "Attempted to construct a reference element in tuple from a "
408 "possible temporary");
409 __value_ = _VSTD::addressof(__val);
412 template <class _That>
413 _LIBCPP_INLINE_VISIBILITY
414 void __construct_from(_That&& __opt)
416 if (__opt.has_value())
417 __construct(_VSTD::forward<_That>(__opt).__get());
420 template <class _That>
421 _LIBCPP_INLINE_VISIBILITY
422 void __assign_from(_That&& __opt)
424 if (has_value() == __opt.has_value())
427 *__value_ = _VSTD::forward<_That>(__opt).__get();
434 __construct(_VSTD::forward<_That>(__opt).__get());
439 template <class _Tp, bool = is_trivially_copyable<_Tp>::value>
440 struct __optional_storage;
443 struct __optional_storage<_Tp, true> : __optional_storage_base<_Tp>
445 using __optional_storage_base<_Tp>::__optional_storage_base;
449 struct __optional_storage<_Tp, false> : __optional_storage_base<_Tp>
451 using value_type = _Tp;
452 using __optional_storage_base<_Tp>::__optional_storage_base;
454 _LIBCPP_INLINE_VISIBILITY
455 __optional_storage() = default;
457 _LIBCPP_INLINE_VISIBILITY
458 __optional_storage(const __optional_storage& __opt)
460 this->__construct_from(__opt);
463 _LIBCPP_INLINE_VISIBILITY
464 __optional_storage(__optional_storage&& __opt)
465 noexcept(is_nothrow_move_constructible_v<value_type>)
467 this->__construct_from(_VSTD::move(__opt));
470 _LIBCPP_INLINE_VISIBILITY
471 __optional_storage& operator=(const __optional_storage& __opt)
473 this->__assign_from(__opt);
477 _LIBCPP_INLINE_VISIBILITY
478 __optional_storage& operator=(__optional_storage&& __opt)
479 noexcept(is_nothrow_move_assignable_v<value_type> &&
480 is_nothrow_move_constructible_v<value_type>)
482 this->__assign_from(_VSTD::move(__opt));
488 using __optional_sfinae_ctor_base_t = __sfinae_ctor_base<
489 is_copy_constructible<_Tp>::value,
490 is_move_constructible<_Tp>::value
494 using __optional_sfinae_assign_base_t = __sfinae_assign_base<
495 (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),
496 (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value)
501 : private __optional_storage<_Tp>
502 , private __optional_sfinae_ctor_base_t<_Tp>
503 , private __optional_sfinae_assign_base_t<_Tp>
505 using __base = __optional_storage<_Tp>;
507 using value_type = _Tp;
510 // Disable the reference extension using this static assert.
511 static_assert(!is_same_v<value_type, in_place_t>,
512 "instantiation of optional with in_place_t is ill-formed");
513 static_assert(!is_same_v<__uncvref_t<value_type>, nullopt_t>,
514 "instantiation of optional with nullopt_t is ill-formed");
515 static_assert(!is_reference_v<value_type>,
516 "instantiation of optional with a reference type is ill-formed");
517 static_assert(is_destructible_v<value_type>,
518 "instantiation of optional with a non-destructible type is ill-formed");
520 // LWG2756: conditionally explicit conversion from _Up
521 struct _CheckOptionalArgsConstructor {
523 static constexpr bool __enable_implicit() {
524 return is_constructible_v<_Tp, _Up&&> &&
525 is_convertible_v<_Up&&, _Tp>;
529 static constexpr bool __enable_explicit() {
530 return is_constructible_v<_Tp, _Up&&> &&
531 !is_convertible_v<_Up&&, _Tp>;
535 using _CheckOptionalArgsCtor = conditional_t<
536 !is_same_v<decay_t<_Up>, in_place_t> &&
537 !is_same_v<decay_t<_Up>, optional>,
538 _CheckOptionalArgsConstructor,
539 __check_tuple_constructor_fail
541 template <class _QualUp>
542 struct _CheckOptionalLikeConstructor {
543 template <class _Up, class _Opt = optional<_Up>>
544 using __check_constructible_from_opt = __lazy_or<
545 is_constructible<_Tp, _Opt&>,
546 is_constructible<_Tp, _Opt const&>,
547 is_constructible<_Tp, _Opt&&>,
548 is_constructible<_Tp, _Opt const&&>,
549 is_convertible<_Opt&, _Tp>,
550 is_convertible<_Opt const&, _Tp>,
551 is_convertible<_Opt&&, _Tp>,
552 is_convertible<_Opt const&&, _Tp>
554 template <class _Up, class _Opt = optional<_Up>>
555 using __check_assignable_from_opt = __lazy_or<
556 is_assignable<_Tp&, _Opt&>,
557 is_assignable<_Tp&, _Opt const&>,
558 is_assignable<_Tp&, _Opt&&>,
559 is_assignable<_Tp&, _Opt const&&>
561 template <class _Up, class _QUp = _QualUp>
562 static constexpr bool __enable_implicit() {
563 return is_convertible<_QUp, _Tp>::value &&
564 !__check_constructible_from_opt<_Up>::value;
566 template <class _Up, class _QUp = _QualUp>
567 static constexpr bool __enable_explicit() {
568 return !is_convertible<_QUp, _Tp>::value &&
569 !__check_constructible_from_opt<_Up>::value;
571 template <class _Up, class _QUp = _QualUp>
572 static constexpr bool __enable_assign() {
573 // Construction and assignability of _Qup to _Tp has already been
575 return !__check_constructible_from_opt<_Up>::value &&
576 !__check_assignable_from_opt<_Up>::value;
580 template <class _Up, class _QualUp>
581 using _CheckOptionalLikeCtor = conditional_t<
583 __lazy_not<is_same<_Up, _Tp>>,
584 is_constructible<_Tp, _QualUp>
586 _CheckOptionalLikeConstructor<_QualUp>,
587 __check_tuple_constructor_fail
589 template <class _Up, class _QualUp>
590 using _CheckOptionalLikeAssign = conditional_t<
592 __lazy_not<is_same<_Up, _Tp>>,
593 is_constructible<_Tp, _QualUp>,
594 is_assignable<_Tp&, _QualUp>
596 _CheckOptionalLikeConstructor<_QualUp>,
597 __check_tuple_constructor_fail
601 _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
602 _LIBCPP_INLINE_VISIBILITY optional(const optional&) = default;
603 _LIBCPP_INLINE_VISIBILITY optional(optional&&) = default;
604 _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
606 template <class... _Args, class = enable_if_t<
607 is_constructible_v<value_type, _Args...>>
609 _LIBCPP_INLINE_VISIBILITY
610 constexpr explicit optional(in_place_t, _Args&&... __args)
611 : __base(in_place, _VSTD::forward<_Args>(__args)...) {}
613 template <class _Up, class... _Args, class = enable_if_t<
614 is_constructible_v<value_type, initializer_list<_Up>&, _Args...>>
616 _LIBCPP_INLINE_VISIBILITY
617 constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
618 : __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {}
620 template <class _Up = value_type, enable_if_t<
621 _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>()
623 _LIBCPP_INLINE_VISIBILITY
624 constexpr optional(_Up&& __v)
625 : __base(in_place, _VSTD::forward<_Up>(__v)) {}
627 template <class _Up, enable_if_t<
628 _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>()
630 _LIBCPP_INLINE_VISIBILITY
631 constexpr explicit optional(_Up&& __v)
632 : __base(in_place, _VSTD::forward<_Up>(__v)) {}
634 // LWG2756: conditionally explicit conversion from const optional<_Up>&
635 template <class _Up, enable_if_t<
636 _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>()
638 _LIBCPP_INLINE_VISIBILITY
639 optional(const optional<_Up>& __v)
641 this->__construct_from(__v);
643 template <class _Up, enable_if_t<
644 _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>()
646 _LIBCPP_INLINE_VISIBILITY
647 explicit optional(const optional<_Up>& __v)
649 this->__construct_from(__v);
652 // LWG2756: conditionally explicit conversion from optional<_Up>&&
653 template <class _Up, enable_if_t<
654 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_implicit<_Up>()
656 _LIBCPP_INLINE_VISIBILITY
657 optional(optional<_Up>&& __v)
659 this->__construct_from(_VSTD::move(__v));
661 template <class _Up, enable_if_t<
662 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_explicit<_Up>()
664 _LIBCPP_INLINE_VISIBILITY
665 explicit optional(optional<_Up>&& __v)
667 this->__construct_from(_VSTD::move(__v));
670 _LIBCPP_INLINE_VISIBILITY
671 optional& operator=(nullopt_t) noexcept
677 _LIBCPP_INLINE_VISIBILITY optional& operator=(const optional&) = default;
678 _LIBCPP_INLINE_VISIBILITY optional& operator=(optional&&) = default;
681 template <class _Up = value_type,
684 integral_constant<bool,
685 !is_same_v<decay_t<_Up>, optional> &&
686 !(is_same_v<_Up, value_type> && is_scalar_v<value_type>)
688 is_constructible<value_type, _Up>,
689 is_assignable<value_type&, _Up>
692 _LIBCPP_INLINE_VISIBILITY
696 if (this->has_value())
697 this->__get() = _VSTD::forward<_Up>(__v);
699 this->__construct(_VSTD::forward<_Up>(__v));
704 template <class _Up, enable_if_t<
705 _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>()
707 _LIBCPP_INLINE_VISIBILITY
709 operator=(const optional<_Up>& __v)
711 this->__assign_from(__v);
716 template <class _Up, enable_if_t<
717 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_assign<_Up>()
719 _LIBCPP_INLINE_VISIBILITY
721 operator=(optional<_Up>&& __v)
723 this->__assign_from(_VSTD::move(__v));
727 template <class... _Args,
730 is_constructible_v<value_type, _Args...>
733 _LIBCPP_INLINE_VISIBILITY
735 emplace(_Args&&... __args)
738 this->__construct(_VSTD::forward<_Args>(__args)...);
739 return this->__get();
742 template <class _Up, class... _Args,
745 is_constructible_v<value_type, initializer_list<_Up>&, _Args...>
748 _LIBCPP_INLINE_VISIBILITY
750 emplace(initializer_list<_Up> __il, _Args&&... __args)
753 this->__construct(__il, _VSTD::forward<_Args>(__args)...);
754 return this->__get();
757 _LIBCPP_INLINE_VISIBILITY
758 void swap(optional& __opt)
759 noexcept(is_nothrow_move_constructible_v<value_type> &&
760 is_nothrow_swappable_v<value_type>)
762 if (this->has_value() == __opt.has_value())
765 if (this->has_value())
766 swap(this->__get(), __opt.__get());
770 if (this->has_value())
772 __opt.__construct(_VSTD::move(this->__get()));
777 this->__construct(_VSTD::move(__opt.__get()));
783 _LIBCPP_INLINE_VISIBILITY
785 add_pointer_t<value_type const>
788 _LIBCPP_ASSERT(this->has_value(), "optional operator-> called for disengaged value");
789 #ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
790 return _VSTD::addressof(this->__get());
792 return __operator_arrow(__has_operator_addressof<value_type>{}, this->__get());
796 _LIBCPP_INLINE_VISIBILITY
798 add_pointer_t<value_type>
801 _LIBCPP_ASSERT(this->has_value(), "optional operator-> called for disengaged value");
802 #ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
803 return _VSTD::addressof(this->__get());
805 return __operator_arrow(__has_operator_addressof<value_type>{}, this->__get());
809 _LIBCPP_INLINE_VISIBILITY
814 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
815 return this->__get();
818 _LIBCPP_INLINE_VISIBILITY
823 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
824 return this->__get();
827 _LIBCPP_INLINE_VISIBILITY
832 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
833 return _VSTD::move(this->__get());
836 _LIBCPP_INLINE_VISIBILITY
841 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
842 return _VSTD::move(this->__get());
845 _LIBCPP_INLINE_VISIBILITY
846 constexpr explicit operator bool() const noexcept { return has_value(); }
848 using __base::has_value;
851 _LIBCPP_INLINE_VISIBILITY
852 constexpr value_type const& value() const&
854 if (!this->has_value())
855 __throw_bad_optional_access();
856 return this->__get();
859 _LIBCPP_INLINE_VISIBILITY
860 constexpr value_type& value() &
862 if (!this->has_value())
863 __throw_bad_optional_access();
864 return this->__get();
867 _LIBCPP_INLINE_VISIBILITY
868 constexpr value_type&& value() &&
870 if (!this->has_value())
871 __throw_bad_optional_access();
872 return _VSTD::move(this->__get());
875 _LIBCPP_INLINE_VISIBILITY
876 constexpr value_type const&& value() const&&
878 if (!this->has_value())
879 __throw_bad_optional_access();
880 return _VSTD::move(this->__get());
884 _LIBCPP_INLINE_VISIBILITY
885 constexpr value_type value_or(_Up&& __v) const&
887 static_assert(is_copy_constructible_v<value_type>,
888 "optional<T>::value_or: T must be copy constructible");
889 static_assert(is_convertible_v<_Up, value_type>,
890 "optional<T>::value_or: U must be convertible to T");
891 return this->has_value() ? this->__get() :
892 static_cast<value_type>(_VSTD::forward<_Up>(__v));
896 _LIBCPP_INLINE_VISIBILITY
897 value_type value_or(_Up&& __v) &&
899 static_assert(is_move_constructible_v<value_type>,
900 "optional<T>::value_or: T must be move constructible");
901 static_assert(is_convertible_v<_Up, value_type>,
902 "optional<T>::value_or: U must be convertible to T");
903 return this->has_value() ? _VSTD::move(this->__get()) :
904 static_cast<value_type>(_VSTD::forward<_Up>(__v));
911 _LIBCPP_INLINE_VISIBILITY
913 __operator_arrow(true_type, _Up& __x)
915 return _VSTD::addressof(__x);
919 _LIBCPP_INLINE_VISIBILITY
920 static constexpr _Up*
921 __operator_arrow(false_type, _Up& __x)
927 // Comparisons between optionals
928 template <class _Tp, class _Up>
929 _LIBCPP_INLINE_VISIBILITY constexpr
931 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
932 _VSTD::declval<const _Up&>()), bool>,
935 operator==(const optional<_Tp>& __x, const optional<_Up>& __y)
937 if (static_cast<bool>(__x) != static_cast<bool>(__y))
939 if (!static_cast<bool>(__x))
944 template <class _Tp, class _Up>
945 _LIBCPP_INLINE_VISIBILITY constexpr
947 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
948 _VSTD::declval<const _Up&>()), bool>,
951 operator!=(const optional<_Tp>& __x, const optional<_Up>& __y)
953 if (static_cast<bool>(__x) != static_cast<bool>(__y))
955 if (!static_cast<bool>(__x))
960 template <class _Tp, class _Up>
961 _LIBCPP_INLINE_VISIBILITY constexpr
963 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
964 _VSTD::declval<const _Up&>()), bool>,
967 operator<(const optional<_Tp>& __x, const optional<_Up>& __y)
969 if (!static_cast<bool>(__y))
971 if (!static_cast<bool>(__x))
976 template <class _Tp, class _Up>
977 _LIBCPP_INLINE_VISIBILITY constexpr
979 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
980 _VSTD::declval<const _Up&>()), bool>,
983 operator>(const optional<_Tp>& __x, const optional<_Up>& __y)
985 if (!static_cast<bool>(__x))
987 if (!static_cast<bool>(__y))
992 template <class _Tp, class _Up>
993 _LIBCPP_INLINE_VISIBILITY constexpr
995 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
996 _VSTD::declval<const _Up&>()), bool>,
999 operator<=(const optional<_Tp>& __x, const optional<_Up>& __y)
1001 if (!static_cast<bool>(__x))
1003 if (!static_cast<bool>(__y))
1005 return *__x <= *__y;
1008 template <class _Tp, class _Up>
1009 _LIBCPP_INLINE_VISIBILITY constexpr
1011 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
1012 _VSTD::declval<const _Up&>()), bool>,
1015 operator>=(const optional<_Tp>& __x, const optional<_Up>& __y)
1017 if (!static_cast<bool>(__y))
1019 if (!static_cast<bool>(__x))
1021 return *__x >= *__y;
1024 // Comparisons with nullopt
1025 template <class _Tp>
1026 _LIBCPP_INLINE_VISIBILITY constexpr
1028 operator==(const optional<_Tp>& __x, nullopt_t) noexcept
1030 return !static_cast<bool>(__x);
1033 template <class _Tp>
1034 _LIBCPP_INLINE_VISIBILITY constexpr
1036 operator==(nullopt_t, const optional<_Tp>& __x) noexcept
1038 return !static_cast<bool>(__x);
1041 template <class _Tp>
1042 _LIBCPP_INLINE_VISIBILITY constexpr
1044 operator!=(const optional<_Tp>& __x, nullopt_t) noexcept
1046 return static_cast<bool>(__x);
1049 template <class _Tp>
1050 _LIBCPP_INLINE_VISIBILITY constexpr
1052 operator!=(nullopt_t, const optional<_Tp>& __x) noexcept
1054 return static_cast<bool>(__x);
1057 template <class _Tp>
1058 _LIBCPP_INLINE_VISIBILITY constexpr
1060 operator<(const optional<_Tp>&, nullopt_t) noexcept
1065 template <class _Tp>
1066 _LIBCPP_INLINE_VISIBILITY constexpr
1068 operator<(nullopt_t, const optional<_Tp>& __x) noexcept
1070 return static_cast<bool>(__x);
1073 template <class _Tp>
1074 _LIBCPP_INLINE_VISIBILITY constexpr
1076 operator<=(const optional<_Tp>& __x, nullopt_t) noexcept
1078 return !static_cast<bool>(__x);
1081 template <class _Tp>
1082 _LIBCPP_INLINE_VISIBILITY constexpr
1084 operator<=(nullopt_t, const optional<_Tp>&) noexcept
1089 template <class _Tp>
1090 _LIBCPP_INLINE_VISIBILITY constexpr
1092 operator>(const optional<_Tp>& __x, nullopt_t) noexcept
1094 return static_cast<bool>(__x);
1097 template <class _Tp>
1098 _LIBCPP_INLINE_VISIBILITY constexpr
1100 operator>(nullopt_t, const optional<_Tp>&) noexcept
1105 template <class _Tp>
1106 _LIBCPP_INLINE_VISIBILITY constexpr
1108 operator>=(const optional<_Tp>&, nullopt_t) noexcept
1113 template <class _Tp>
1114 _LIBCPP_INLINE_VISIBILITY constexpr
1116 operator>=(nullopt_t, const optional<_Tp>& __x) noexcept
1118 return !static_cast<bool>(__x);
1121 // Comparisons with T
1122 template <class _Tp, class _Up>
1123 _LIBCPP_INLINE_VISIBILITY constexpr
1125 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
1126 _VSTD::declval<const _Up&>()), bool>,
1129 operator==(const optional<_Tp>& __x, const _Up& __v)
1131 return static_cast<bool>(__x) ? *__x == __v : false;
1134 template <class _Tp, class _Up>
1135 _LIBCPP_INLINE_VISIBILITY constexpr
1137 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
1138 _VSTD::declval<const _Up&>()), bool>,
1141 operator==(const _Tp& __v, const optional<_Up>& __x)
1143 return static_cast<bool>(__x) ? __v == *__x : false;
1146 template <class _Tp, class _Up>
1147 _LIBCPP_INLINE_VISIBILITY constexpr
1149 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
1150 _VSTD::declval<const _Up&>()), bool>,
1153 operator!=(const optional<_Tp>& __x, const _Up& __v)
1155 return static_cast<bool>(__x) ? *__x != __v : true;
1158 template <class _Tp, class _Up>
1159 _LIBCPP_INLINE_VISIBILITY constexpr
1161 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
1162 _VSTD::declval<const _Up&>()), bool>,
1165 operator!=(const _Tp& __v, const optional<_Up>& __x)
1167 return static_cast<bool>(__x) ? __v != *__x : true;
1170 template <class _Tp, class _Up>
1171 _LIBCPP_INLINE_VISIBILITY constexpr
1173 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
1174 _VSTD::declval<const _Up&>()), bool>,
1177 operator<(const optional<_Tp>& __x, const _Up& __v)
1179 return static_cast<bool>(__x) ? *__x < __v : true;
1182 template <class _Tp, class _Up>
1183 _LIBCPP_INLINE_VISIBILITY constexpr
1185 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
1186 _VSTD::declval<const _Up&>()), bool>,
1189 operator<(const _Tp& __v, const optional<_Up>& __x)
1191 return static_cast<bool>(__x) ? __v < *__x : false;
1194 template <class _Tp, class _Up>
1195 _LIBCPP_INLINE_VISIBILITY constexpr
1197 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
1198 _VSTD::declval<const _Up&>()), bool>,
1201 operator<=(const optional<_Tp>& __x, const _Up& __v)
1203 return static_cast<bool>(__x) ? *__x <= __v : true;
1206 template <class _Tp, class _Up>
1207 _LIBCPP_INLINE_VISIBILITY constexpr
1209 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
1210 _VSTD::declval<const _Up&>()), bool>,
1213 operator<=(const _Tp& __v, const optional<_Up>& __x)
1215 return static_cast<bool>(__x) ? __v <= *__x : false;
1218 template <class _Tp, class _Up>
1219 _LIBCPP_INLINE_VISIBILITY constexpr
1221 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
1222 _VSTD::declval<const _Up&>()), bool>,
1225 operator>(const optional<_Tp>& __x, const _Up& __v)
1227 return static_cast<bool>(__x) ? *__x > __v : false;
1230 template <class _Tp, class _Up>
1231 _LIBCPP_INLINE_VISIBILITY constexpr
1233 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
1234 _VSTD::declval<const _Up&>()), bool>,
1237 operator>(const _Tp& __v, const optional<_Up>& __x)
1239 return static_cast<bool>(__x) ? __v > *__x : true;
1242 template <class _Tp, class _Up>
1243 _LIBCPP_INLINE_VISIBILITY constexpr
1245 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
1246 _VSTD::declval<const _Up&>()), bool>,
1249 operator>=(const optional<_Tp>& __x, const _Up& __v)
1251 return static_cast<bool>(__x) ? *__x >= __v : false;
1254 template <class _Tp, class _Up>
1255 _LIBCPP_INLINE_VISIBILITY constexpr
1257 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
1258 _VSTD::declval<const _Up&>()), bool>,
1261 operator>=(const _Tp& __v, const optional<_Up>& __x)
1263 return static_cast<bool>(__x) ? __v >= *__x : true;
1267 template <class _Tp>
1268 inline _LIBCPP_INLINE_VISIBILITY
1270 is_move_constructible_v<_Tp> && is_swappable_v<_Tp>,
1273 swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y)))
1278 template <class _Tp>
1279 _LIBCPP_INLINE_VISIBILITY constexpr
1280 optional<decay_t<_Tp>> make_optional(_Tp&& __v)
1282 return optional<decay_t<_Tp>>(_VSTD::forward<_Tp>(__v));
1285 template <class _Tp, class... _Args>
1286 _LIBCPP_INLINE_VISIBILITY constexpr
1287 optional<_Tp> make_optional(_Args&&... __args)
1289 return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...);
1292 template <class _Tp, class _Up, class... _Args>
1293 _LIBCPP_INLINE_VISIBILITY constexpr
1294 optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args)
1296 return optional<_Tp>(in_place, __il, _VSTD::forward<_Args>(__args)...);
1299 template <class _Tp>
1300 struct _LIBCPP_TEMPLATE_VIS hash<
1301 __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>>
1304 typedef optional<_Tp> argument_type;
1305 typedef size_t result_type;
1307 _LIBCPP_INLINE_VISIBILITY
1308 result_type operator()(const argument_type& __opt) const
1310 return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
1314 _LIBCPP_END_NAMESPACE_STD
1316 #endif // _LIBCPP_STD_VER > 14
1318 #endif // _LIBCPP_OPTIONAL