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 inline 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 T&, const optional<U>&);
61 template <class T, class U> constexpr bool operator!=(const optional<T>&, const U&);
62 template <class T, class U> constexpr bool operator!=(const T&, const optional<U>&);
63 template <class T, class U> constexpr bool operator<(const optional<T>&, const U&);
64 template <class T, class U> constexpr bool operator<(const T&, const optional<U>&);
65 template <class T, class U> constexpr bool operator<=(const optional<T>&, const U&);
66 template <class T, class U> constexpr bool operator<=(const T&, const optional<U>&);
67 template <class T, class U> constexpr bool operator>(const optional<T>&, const U&);
68 template <class T, class U> constexpr bool operator>(const T&, const optional<U>&);
69 template <class T, class U> constexpr bool operator>=(const optional<T>&, const U&);
70 template <class T, class U> constexpr bool operator>=(const T&, const optional<U>&);
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
144 optional(T) -> optional<T>;
152 #include <__functional_base>
153 #include <functional>
154 #include <initializer_list>
157 #include <type_traits>
160 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
161 #pragma GCC system_header
165 #include <__undef_macros>
168 namespace std // purposefully not using versioning namespace
171 class _LIBCPP_EXCEPTION_ABI bad_optional_access
175 // Get the key function ~bad_optional_access() into the dylib
176 virtual ~bad_optional_access() _NOEXCEPT;
177 virtual const char* what() const _NOEXCEPT;
182 #if _LIBCPP_STD_VER > 14
184 _LIBCPP_BEGIN_NAMESPACE_STD
187 inline _LIBCPP_INLINE_VISIBILITY
188 void __throw_bad_optional_access() {
189 #ifndef _LIBCPP_NO_EXCEPTIONS
190 throw bad_optional_access();
198 struct __secret_tag { _LIBCPP_INLINE_VISIBILITY explicit __secret_tag() = default; };
199 _LIBCPP_INLINE_VISIBILITY constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
202 _LIBCPP_INLINE_VAR constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
204 template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
205 struct __optional_destruct_base;
208 struct __optional_destruct_base<_Tp, false>
210 typedef _Tp value_type;
211 static_assert(is_object_v<value_type>,
212 "instantiation of optional with a non-object type is undefined behavior");
220 _LIBCPP_INLINE_VISIBILITY
221 ~__optional_destruct_base()
224 __val_.~value_type();
227 _LIBCPP_INLINE_VISIBILITY
228 constexpr __optional_destruct_base() noexcept
232 template <class... _Args>
233 _LIBCPP_INLINE_VISIBILITY
234 constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
235 : __val_(_VSTD::forward<_Args>(__args)...),
238 _LIBCPP_INLINE_VISIBILITY
239 void reset() noexcept
243 __val_.~value_type();
250 struct __optional_destruct_base<_Tp, true>
252 typedef _Tp value_type;
253 static_assert(is_object_v<value_type>,
254 "instantiation of optional with a non-object type is undefined behavior");
262 _LIBCPP_INLINE_VISIBILITY
263 constexpr __optional_destruct_base() noexcept
267 template <class... _Args>
268 _LIBCPP_INLINE_VISIBILITY
269 constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
270 : __val_(_VSTD::forward<_Args>(__args)...),
273 _LIBCPP_INLINE_VISIBILITY
274 void reset() noexcept
283 template <class _Tp, bool = is_reference<_Tp>::value>
284 struct __optional_storage_base : __optional_destruct_base<_Tp>
286 using __base = __optional_destruct_base<_Tp>;
287 using value_type = _Tp;
288 using __base::__base;
290 _LIBCPP_INLINE_VISIBILITY
291 constexpr bool has_value() const noexcept
293 return this->__engaged_;
296 _LIBCPP_INLINE_VISIBILITY
297 constexpr value_type& __get() & noexcept
301 _LIBCPP_INLINE_VISIBILITY
302 constexpr const value_type& __get() const& noexcept
306 _LIBCPP_INLINE_VISIBILITY
307 constexpr value_type&& __get() && noexcept
309 return _VSTD::move(this->__val_);
311 _LIBCPP_INLINE_VISIBILITY
312 constexpr const value_type&& __get() const&& noexcept
314 return _VSTD::move(this->__val_);
317 template <class... _Args>
318 _LIBCPP_INLINE_VISIBILITY
319 void __construct(_Args&&... __args)
321 _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage");
322 ::new((void*)_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Args>(__args)...);
323 this->__engaged_ = true;
326 template <class _That>
327 _LIBCPP_INLINE_VISIBILITY
328 void __construct_from(_That&& __opt)
330 if (__opt.has_value())
331 __construct(_VSTD::forward<_That>(__opt).__get());
334 template <class _That>
335 _LIBCPP_INLINE_VISIBILITY
336 void __assign_from(_That&& __opt)
338 if (this->__engaged_ == __opt.has_value())
340 if (this->__engaged_)
341 this->__val_ = _VSTD::forward<_That>(__opt).__get();
345 if (this->__engaged_)
348 __construct(_VSTD::forward<_That>(__opt).__get());
353 // optional<T&> is currently required ill-formed, however it may to be in the
354 // future. For this reason it has already been implemented to ensure we can
355 // make the change in an ABI compatible manner.
357 struct __optional_storage_base<_Tp, true>
359 using value_type = _Tp;
360 using __raw_type = remove_reference_t<_Tp>;
361 __raw_type* __value_;
364 static constexpr bool __can_bind_reference() {
365 using _RawUp = typename remove_reference<_Up>::type;
366 using _UpPtr = _RawUp*;
367 using _RawTp = typename remove_reference<_Tp>::type;
368 using _TpPtr = _RawTp*;
369 using _CheckLValueArg = integral_constant<bool,
370 (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value)
371 || is_same<_RawUp, reference_wrapper<_RawTp>>::value
372 || is_same<_RawUp, reference_wrapper<typename remove_const<_RawTp>::type>>::value
374 return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value)
375 || (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value &&
376 is_convertible<_UpPtr, _TpPtr>::value);
379 _LIBCPP_INLINE_VISIBILITY
380 constexpr __optional_storage_base() noexcept
381 : __value_(nullptr) {}
383 template <class _UArg>
384 _LIBCPP_INLINE_VISIBILITY
385 constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg)
386 : __value_(_VSTD::addressof(__uarg))
388 static_assert(__can_bind_reference<_UArg>(),
389 "Attempted to construct a reference element in tuple from a "
390 "possible temporary");
393 _LIBCPP_INLINE_VISIBILITY
394 void reset() noexcept { __value_ = nullptr; }
396 _LIBCPP_INLINE_VISIBILITY
397 constexpr bool has_value() const noexcept
398 { return __value_ != nullptr; }
400 _LIBCPP_INLINE_VISIBILITY
401 constexpr value_type& __get() const& noexcept
402 { return *__value_; }
404 _LIBCPP_INLINE_VISIBILITY
405 constexpr value_type&& __get() const&& noexcept
406 { return _VSTD::forward<value_type>(*__value_); }
408 template <class _UArg>
409 _LIBCPP_INLINE_VISIBILITY
410 void __construct(_UArg&& __val)
412 _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage");
413 static_assert(__can_bind_reference<_UArg>(),
414 "Attempted to construct a reference element in tuple from a "
415 "possible temporary");
416 __value_ = _VSTD::addressof(__val);
419 template <class _That>
420 _LIBCPP_INLINE_VISIBILITY
421 void __construct_from(_That&& __opt)
423 if (__opt.has_value())
424 __construct(_VSTD::forward<_That>(__opt).__get());
427 template <class _That>
428 _LIBCPP_INLINE_VISIBILITY
429 void __assign_from(_That&& __opt)
431 if (has_value() == __opt.has_value())
434 *__value_ = _VSTD::forward<_That>(__opt).__get();
441 __construct(_VSTD::forward<_That>(__opt).__get());
446 template <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value>
447 struct __optional_copy_base : __optional_storage_base<_Tp>
449 using __optional_storage_base<_Tp>::__optional_storage_base;
453 struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp>
455 using __optional_storage_base<_Tp>::__optional_storage_base;
457 _LIBCPP_INLINE_VISIBILITY
458 __optional_copy_base() = default;
460 _LIBCPP_INLINE_VISIBILITY
461 __optional_copy_base(const __optional_copy_base& __opt)
463 this->__construct_from(__opt);
466 _LIBCPP_INLINE_VISIBILITY
467 __optional_copy_base(__optional_copy_base&&) = default;
468 _LIBCPP_INLINE_VISIBILITY
469 __optional_copy_base& operator=(const __optional_copy_base&) = default;
470 _LIBCPP_INLINE_VISIBILITY
471 __optional_copy_base& operator=(__optional_copy_base&&) = default;
474 template <class _Tp, bool = is_trivially_move_constructible<_Tp>::value>
475 struct __optional_move_base : __optional_copy_base<_Tp>
477 using __optional_copy_base<_Tp>::__optional_copy_base;
481 struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp>
483 using value_type = _Tp;
484 using __optional_copy_base<_Tp>::__optional_copy_base;
486 _LIBCPP_INLINE_VISIBILITY
487 __optional_move_base() = default;
488 _LIBCPP_INLINE_VISIBILITY
489 __optional_move_base(const __optional_move_base&) = default;
491 _LIBCPP_INLINE_VISIBILITY
492 __optional_move_base(__optional_move_base&& __opt)
493 noexcept(is_nothrow_move_constructible_v<value_type>)
495 this->__construct_from(_VSTD::move(__opt));
498 _LIBCPP_INLINE_VISIBILITY
499 __optional_move_base& operator=(const __optional_move_base&) = default;
500 _LIBCPP_INLINE_VISIBILITY
501 __optional_move_base& operator=(__optional_move_base&&) = default;
504 template <class _Tp, bool =
505 is_trivially_destructible<_Tp>::value &&
506 is_trivially_copy_constructible<_Tp>::value &&
507 is_trivially_copy_assignable<_Tp>::value>
508 struct __optional_copy_assign_base : __optional_move_base<_Tp>
510 using __optional_move_base<_Tp>::__optional_move_base;
514 struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp>
516 using __optional_move_base<_Tp>::__optional_move_base;
518 _LIBCPP_INLINE_VISIBILITY
519 __optional_copy_assign_base() = default;
520 _LIBCPP_INLINE_VISIBILITY
521 __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
522 _LIBCPP_INLINE_VISIBILITY
523 __optional_copy_assign_base(__optional_copy_assign_base&&) = default;
525 _LIBCPP_INLINE_VISIBILITY
526 __optional_copy_assign_base& operator=(const __optional_copy_assign_base& __opt)
528 this->__assign_from(__opt);
532 _LIBCPP_INLINE_VISIBILITY
533 __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
536 template <class _Tp, bool =
537 is_trivially_destructible<_Tp>::value &&
538 is_trivially_move_constructible<_Tp>::value &&
539 is_trivially_move_assignable<_Tp>::value>
540 struct __optional_move_assign_base : __optional_copy_assign_base<_Tp>
542 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
546 struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp>
548 using value_type = _Tp;
549 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
551 _LIBCPP_INLINE_VISIBILITY
552 __optional_move_assign_base() = default;
553 _LIBCPP_INLINE_VISIBILITY
554 __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;
555 _LIBCPP_INLINE_VISIBILITY
556 __optional_move_assign_base(__optional_move_assign_base&&) = default;
557 _LIBCPP_INLINE_VISIBILITY
558 __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
560 _LIBCPP_INLINE_VISIBILITY
561 __optional_move_assign_base& operator=(__optional_move_assign_base&& __opt)
562 noexcept(is_nothrow_move_assignable_v<value_type> &&
563 is_nothrow_move_constructible_v<value_type>)
565 this->__assign_from(_VSTD::move(__opt));
571 using __optional_sfinae_ctor_base_t = __sfinae_ctor_base<
572 is_copy_constructible<_Tp>::value,
573 is_move_constructible<_Tp>::value
577 using __optional_sfinae_assign_base_t = __sfinae_assign_base<
578 (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),
579 (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value)
584 : private __optional_move_assign_base<_Tp>
585 , private __optional_sfinae_ctor_base_t<_Tp>
586 , private __optional_sfinae_assign_base_t<_Tp>
588 using __base = __optional_move_assign_base<_Tp>;
590 using value_type = _Tp;
593 // Disable the reference extension using this static assert.
594 static_assert(!is_same_v<value_type, in_place_t>,
595 "instantiation of optional with in_place_t is ill-formed");
596 static_assert(!is_same_v<__uncvref_t<value_type>, nullopt_t>,
597 "instantiation of optional with nullopt_t is ill-formed");
598 static_assert(!is_reference_v<value_type>,
599 "instantiation of optional with a reference type is ill-formed");
600 static_assert(is_destructible_v<value_type>,
601 "instantiation of optional with a non-destructible type is ill-formed");
603 // LWG2756: conditionally explicit conversion from _Up
604 struct _CheckOptionalArgsConstructor {
606 static constexpr bool __enable_implicit() {
607 return is_constructible_v<_Tp, _Up&&> &&
608 is_convertible_v<_Up&&, _Tp>;
612 static constexpr bool __enable_explicit() {
613 return is_constructible_v<_Tp, _Up&&> &&
614 !is_convertible_v<_Up&&, _Tp>;
618 using _CheckOptionalArgsCtor = conditional_t<
619 !is_same_v<__uncvref_t<_Up>, in_place_t> &&
620 !is_same_v<__uncvref_t<_Up>, optional>,
621 _CheckOptionalArgsConstructor,
622 __check_tuple_constructor_fail
624 template <class _QualUp>
625 struct _CheckOptionalLikeConstructor {
626 template <class _Up, class _Opt = optional<_Up>>
627 using __check_constructible_from_opt = __lazy_or<
628 is_constructible<_Tp, _Opt&>,
629 is_constructible<_Tp, _Opt const&>,
630 is_constructible<_Tp, _Opt&&>,
631 is_constructible<_Tp, _Opt const&&>,
632 is_convertible<_Opt&, _Tp>,
633 is_convertible<_Opt const&, _Tp>,
634 is_convertible<_Opt&&, _Tp>,
635 is_convertible<_Opt const&&, _Tp>
637 template <class _Up, class _Opt = optional<_Up>>
638 using __check_assignable_from_opt = __lazy_or<
639 is_assignable<_Tp&, _Opt&>,
640 is_assignable<_Tp&, _Opt const&>,
641 is_assignable<_Tp&, _Opt&&>,
642 is_assignable<_Tp&, _Opt const&&>
644 template <class _Up, class _QUp = _QualUp>
645 static constexpr bool __enable_implicit() {
646 return is_convertible<_QUp, _Tp>::value &&
647 !__check_constructible_from_opt<_Up>::value;
649 template <class _Up, class _QUp = _QualUp>
650 static constexpr bool __enable_explicit() {
651 return !is_convertible<_QUp, _Tp>::value &&
652 !__check_constructible_from_opt<_Up>::value;
654 template <class _Up, class _QUp = _QualUp>
655 static constexpr bool __enable_assign() {
656 // Construction and assignability of _Qup to _Tp has already been
658 return !__check_constructible_from_opt<_Up>::value &&
659 !__check_assignable_from_opt<_Up>::value;
663 template <class _Up, class _QualUp>
664 using _CheckOptionalLikeCtor = conditional_t<
666 __lazy_not<is_same<_Up, _Tp>>,
667 is_constructible<_Tp, _QualUp>
669 _CheckOptionalLikeConstructor<_QualUp>,
670 __check_tuple_constructor_fail
672 template <class _Up, class _QualUp>
673 using _CheckOptionalLikeAssign = conditional_t<
675 __lazy_not<is_same<_Up, _Tp>>,
676 is_constructible<_Tp, _QualUp>,
677 is_assignable<_Tp&, _QualUp>
679 _CheckOptionalLikeConstructor<_QualUp>,
680 __check_tuple_constructor_fail
684 _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
685 _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
686 _LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default;
687 _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
689 template <class... _Args, class = enable_if_t<
690 is_constructible_v<value_type, _Args...>>
692 _LIBCPP_INLINE_VISIBILITY
693 constexpr explicit optional(in_place_t, _Args&&... __args)
694 : __base(in_place, _VSTD::forward<_Args>(__args)...) {}
696 template <class _Up, class... _Args, class = enable_if_t<
697 is_constructible_v<value_type, initializer_list<_Up>&, _Args...>>
699 _LIBCPP_INLINE_VISIBILITY
700 constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
701 : __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {}
703 template <class _Up = value_type, enable_if_t<
704 _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>()
706 _LIBCPP_INLINE_VISIBILITY
707 constexpr optional(_Up&& __v)
708 : __base(in_place, _VSTD::forward<_Up>(__v)) {}
710 template <class _Up, enable_if_t<
711 _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>()
713 _LIBCPP_INLINE_VISIBILITY
714 constexpr explicit optional(_Up&& __v)
715 : __base(in_place, _VSTD::forward<_Up>(__v)) {}
717 // LWG2756: conditionally explicit conversion from const optional<_Up>&
718 template <class _Up, enable_if_t<
719 _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>()
721 _LIBCPP_INLINE_VISIBILITY
722 optional(const optional<_Up>& __v)
724 this->__construct_from(__v);
726 template <class _Up, enable_if_t<
727 _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>()
729 _LIBCPP_INLINE_VISIBILITY
730 explicit optional(const optional<_Up>& __v)
732 this->__construct_from(__v);
735 // LWG2756: conditionally explicit conversion from optional<_Up>&&
736 template <class _Up, enable_if_t<
737 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_implicit<_Up>()
739 _LIBCPP_INLINE_VISIBILITY
740 optional(optional<_Up>&& __v)
742 this->__construct_from(_VSTD::move(__v));
744 template <class _Up, enable_if_t<
745 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_explicit<_Up>()
747 _LIBCPP_INLINE_VISIBILITY
748 explicit optional(optional<_Up>&& __v)
750 this->__construct_from(_VSTD::move(__v));
753 _LIBCPP_INLINE_VISIBILITY
754 optional& operator=(nullopt_t) noexcept
760 _LIBCPP_INLINE_VISIBILITY optional& operator=(const optional&) = default;
761 _LIBCPP_INLINE_VISIBILITY optional& operator=(optional&&) = default;
764 template <class _Up = value_type,
767 integral_constant<bool,
768 !is_same_v<__uncvref_t<_Up>, optional> &&
769 !(is_same_v<_Up, value_type> && is_scalar_v<value_type>)
771 is_constructible<value_type, _Up>,
772 is_assignable<value_type&, _Up>
775 _LIBCPP_INLINE_VISIBILITY
779 if (this->has_value())
780 this->__get() = _VSTD::forward<_Up>(__v);
782 this->__construct(_VSTD::forward<_Up>(__v));
787 template <class _Up, enable_if_t<
788 _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>()
790 _LIBCPP_INLINE_VISIBILITY
792 operator=(const optional<_Up>& __v)
794 this->__assign_from(__v);
799 template <class _Up, enable_if_t<
800 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_assign<_Up>()
802 _LIBCPP_INLINE_VISIBILITY
804 operator=(optional<_Up>&& __v)
806 this->__assign_from(_VSTD::move(__v));
810 template <class... _Args,
813 is_constructible_v<value_type, _Args...>
816 _LIBCPP_INLINE_VISIBILITY
818 emplace(_Args&&... __args)
821 this->__construct(_VSTD::forward<_Args>(__args)...);
822 return this->__get();
825 template <class _Up, class... _Args,
828 is_constructible_v<value_type, initializer_list<_Up>&, _Args...>
831 _LIBCPP_INLINE_VISIBILITY
833 emplace(initializer_list<_Up> __il, _Args&&... __args)
836 this->__construct(__il, _VSTD::forward<_Args>(__args)...);
837 return this->__get();
840 _LIBCPP_INLINE_VISIBILITY
841 void swap(optional& __opt)
842 noexcept(is_nothrow_move_constructible_v<value_type> &&
843 is_nothrow_swappable_v<value_type>)
845 if (this->has_value() == __opt.has_value())
848 if (this->has_value())
849 swap(this->__get(), __opt.__get());
853 if (this->has_value())
855 __opt.__construct(_VSTD::move(this->__get()));
860 this->__construct(_VSTD::move(__opt.__get()));
866 _LIBCPP_INLINE_VISIBILITY
868 add_pointer_t<value_type const>
871 _LIBCPP_ASSERT(this->has_value(), "optional operator-> called for disengaged value");
872 #ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
873 return _VSTD::addressof(this->__get());
875 return __operator_arrow(__has_operator_addressof<value_type>{}, this->__get());
879 _LIBCPP_INLINE_VISIBILITY
881 add_pointer_t<value_type>
884 _LIBCPP_ASSERT(this->has_value(), "optional operator-> called for disengaged value");
885 #ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
886 return _VSTD::addressof(this->__get());
888 return __operator_arrow(__has_operator_addressof<value_type>{}, this->__get());
892 _LIBCPP_INLINE_VISIBILITY
897 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
898 return this->__get();
901 _LIBCPP_INLINE_VISIBILITY
906 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
907 return this->__get();
910 _LIBCPP_INLINE_VISIBILITY
915 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
916 return _VSTD::move(this->__get());
919 _LIBCPP_INLINE_VISIBILITY
924 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
925 return _VSTD::move(this->__get());
928 _LIBCPP_INLINE_VISIBILITY
929 constexpr explicit operator bool() const noexcept { return has_value(); }
931 using __base::has_value;
934 _LIBCPP_INLINE_VISIBILITY
935 constexpr value_type const& value() const&
937 if (!this->has_value())
938 __throw_bad_optional_access();
939 return this->__get();
942 _LIBCPP_INLINE_VISIBILITY
943 constexpr value_type& value() &
945 if (!this->has_value())
946 __throw_bad_optional_access();
947 return this->__get();
950 _LIBCPP_INLINE_VISIBILITY
951 constexpr value_type&& value() &&
953 if (!this->has_value())
954 __throw_bad_optional_access();
955 return _VSTD::move(this->__get());
958 _LIBCPP_INLINE_VISIBILITY
959 constexpr value_type const&& value() const&&
961 if (!this->has_value())
962 __throw_bad_optional_access();
963 return _VSTD::move(this->__get());
967 _LIBCPP_INLINE_VISIBILITY
968 constexpr value_type value_or(_Up&& __v) const&
970 static_assert(is_copy_constructible_v<value_type>,
971 "optional<T>::value_or: T must be copy constructible");
972 static_assert(is_convertible_v<_Up, value_type>,
973 "optional<T>::value_or: U must be convertible to T");
974 return this->has_value() ? this->__get() :
975 static_cast<value_type>(_VSTD::forward<_Up>(__v));
979 _LIBCPP_INLINE_VISIBILITY
980 constexpr value_type value_or(_Up&& __v) &&
982 static_assert(is_move_constructible_v<value_type>,
983 "optional<T>::value_or: T must be move constructible");
984 static_assert(is_convertible_v<_Up, value_type>,
985 "optional<T>::value_or: U must be convertible to T");
986 return this->has_value() ? _VSTD::move(this->__get()) :
987 static_cast<value_type>(_VSTD::forward<_Up>(__v));
994 _LIBCPP_INLINE_VISIBILITY
996 __operator_arrow(true_type, _Up& __x)
998 return _VSTD::addressof(__x);
1001 template <class _Up>
1002 _LIBCPP_INLINE_VISIBILITY
1003 static constexpr _Up*
1004 __operator_arrow(false_type, _Up& __x)
1010 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1012 optional(T) -> optional<T>;
1015 // Comparisons between optionals
1016 template <class _Tp, class _Up>
1017 _LIBCPP_INLINE_VISIBILITY constexpr
1019 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
1020 _VSTD::declval<const _Up&>()), bool>,
1023 operator==(const optional<_Tp>& __x, const optional<_Up>& __y)
1025 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1027 if (!static_cast<bool>(__x))
1029 return *__x == *__y;
1032 template <class _Tp, class _Up>
1033 _LIBCPP_INLINE_VISIBILITY constexpr
1035 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
1036 _VSTD::declval<const _Up&>()), bool>,
1039 operator!=(const optional<_Tp>& __x, const optional<_Up>& __y)
1041 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1043 if (!static_cast<bool>(__x))
1045 return *__x != *__y;
1048 template <class _Tp, class _Up>
1049 _LIBCPP_INLINE_VISIBILITY constexpr
1051 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
1052 _VSTD::declval<const _Up&>()), bool>,
1055 operator<(const optional<_Tp>& __x, const optional<_Up>& __y)
1057 if (!static_cast<bool>(__y))
1059 if (!static_cast<bool>(__x))
1064 template <class _Tp, class _Up>
1065 _LIBCPP_INLINE_VISIBILITY constexpr
1067 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
1068 _VSTD::declval<const _Up&>()), bool>,
1071 operator>(const optional<_Tp>& __x, const optional<_Up>& __y)
1073 if (!static_cast<bool>(__x))
1075 if (!static_cast<bool>(__y))
1080 template <class _Tp, class _Up>
1081 _LIBCPP_INLINE_VISIBILITY constexpr
1083 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
1084 _VSTD::declval<const _Up&>()), bool>,
1087 operator<=(const optional<_Tp>& __x, const optional<_Up>& __y)
1089 if (!static_cast<bool>(__x))
1091 if (!static_cast<bool>(__y))
1093 return *__x <= *__y;
1096 template <class _Tp, class _Up>
1097 _LIBCPP_INLINE_VISIBILITY constexpr
1099 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
1100 _VSTD::declval<const _Up&>()), bool>,
1103 operator>=(const optional<_Tp>& __x, const optional<_Up>& __y)
1105 if (!static_cast<bool>(__y))
1107 if (!static_cast<bool>(__x))
1109 return *__x >= *__y;
1112 // Comparisons with nullopt
1113 template <class _Tp>
1114 _LIBCPP_INLINE_VISIBILITY constexpr
1116 operator==(const optional<_Tp>& __x, nullopt_t) noexcept
1118 return !static_cast<bool>(__x);
1121 template <class _Tp>
1122 _LIBCPP_INLINE_VISIBILITY constexpr
1124 operator==(nullopt_t, const optional<_Tp>& __x) noexcept
1126 return !static_cast<bool>(__x);
1129 template <class _Tp>
1130 _LIBCPP_INLINE_VISIBILITY constexpr
1132 operator!=(const optional<_Tp>& __x, nullopt_t) noexcept
1134 return static_cast<bool>(__x);
1137 template <class _Tp>
1138 _LIBCPP_INLINE_VISIBILITY constexpr
1140 operator!=(nullopt_t, const optional<_Tp>& __x) noexcept
1142 return static_cast<bool>(__x);
1145 template <class _Tp>
1146 _LIBCPP_INLINE_VISIBILITY constexpr
1148 operator<(const optional<_Tp>&, nullopt_t) noexcept
1153 template <class _Tp>
1154 _LIBCPP_INLINE_VISIBILITY constexpr
1156 operator<(nullopt_t, const optional<_Tp>& __x) noexcept
1158 return static_cast<bool>(__x);
1161 template <class _Tp>
1162 _LIBCPP_INLINE_VISIBILITY constexpr
1164 operator<=(const optional<_Tp>& __x, nullopt_t) noexcept
1166 return !static_cast<bool>(__x);
1169 template <class _Tp>
1170 _LIBCPP_INLINE_VISIBILITY constexpr
1172 operator<=(nullopt_t, const optional<_Tp>&) noexcept
1177 template <class _Tp>
1178 _LIBCPP_INLINE_VISIBILITY constexpr
1180 operator>(const optional<_Tp>& __x, nullopt_t) noexcept
1182 return static_cast<bool>(__x);
1185 template <class _Tp>
1186 _LIBCPP_INLINE_VISIBILITY constexpr
1188 operator>(nullopt_t, const optional<_Tp>&) noexcept
1193 template <class _Tp>
1194 _LIBCPP_INLINE_VISIBILITY constexpr
1196 operator>=(const optional<_Tp>&, nullopt_t) noexcept
1201 template <class _Tp>
1202 _LIBCPP_INLINE_VISIBILITY constexpr
1204 operator>=(nullopt_t, const optional<_Tp>& __x) noexcept
1206 return !static_cast<bool>(__x);
1209 // Comparisons with T
1210 template <class _Tp, class _Up>
1211 _LIBCPP_INLINE_VISIBILITY constexpr
1213 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
1214 _VSTD::declval<const _Up&>()), bool>,
1217 operator==(const optional<_Tp>& __x, const _Up& __v)
1219 return static_cast<bool>(__x) ? *__x == __v : false;
1222 template <class _Tp, class _Up>
1223 _LIBCPP_INLINE_VISIBILITY constexpr
1225 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
1226 _VSTD::declval<const _Up&>()), bool>,
1229 operator==(const _Tp& __v, const optional<_Up>& __x)
1231 return static_cast<bool>(__x) ? __v == *__x : false;
1234 template <class _Tp, class _Up>
1235 _LIBCPP_INLINE_VISIBILITY constexpr
1237 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
1238 _VSTD::declval<const _Up&>()), bool>,
1241 operator!=(const optional<_Tp>& __x, const _Up& __v)
1243 return static_cast<bool>(__x) ? *__x != __v : true;
1246 template <class _Tp, class _Up>
1247 _LIBCPP_INLINE_VISIBILITY constexpr
1249 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
1250 _VSTD::declval<const _Up&>()), bool>,
1253 operator!=(const _Tp& __v, const optional<_Up>& __x)
1255 return static_cast<bool>(__x) ? __v != *__x : true;
1258 template <class _Tp, class _Up>
1259 _LIBCPP_INLINE_VISIBILITY constexpr
1261 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
1262 _VSTD::declval<const _Up&>()), bool>,
1265 operator<(const optional<_Tp>& __x, const _Up& __v)
1267 return static_cast<bool>(__x) ? *__x < __v : true;
1270 template <class _Tp, class _Up>
1271 _LIBCPP_INLINE_VISIBILITY constexpr
1273 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
1274 _VSTD::declval<const _Up&>()), bool>,
1277 operator<(const _Tp& __v, const optional<_Up>& __x)
1279 return static_cast<bool>(__x) ? __v < *__x : false;
1282 template <class _Tp, class _Up>
1283 _LIBCPP_INLINE_VISIBILITY constexpr
1285 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
1286 _VSTD::declval<const _Up&>()), bool>,
1289 operator<=(const optional<_Tp>& __x, const _Up& __v)
1291 return static_cast<bool>(__x) ? *__x <= __v : true;
1294 template <class _Tp, class _Up>
1295 _LIBCPP_INLINE_VISIBILITY constexpr
1297 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
1298 _VSTD::declval<const _Up&>()), bool>,
1301 operator<=(const _Tp& __v, const optional<_Up>& __x)
1303 return static_cast<bool>(__x) ? __v <= *__x : false;
1306 template <class _Tp, class _Up>
1307 _LIBCPP_INLINE_VISIBILITY constexpr
1309 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
1310 _VSTD::declval<const _Up&>()), bool>,
1313 operator>(const optional<_Tp>& __x, const _Up& __v)
1315 return static_cast<bool>(__x) ? *__x > __v : false;
1318 template <class _Tp, class _Up>
1319 _LIBCPP_INLINE_VISIBILITY constexpr
1321 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
1322 _VSTD::declval<const _Up&>()), bool>,
1325 operator>(const _Tp& __v, const optional<_Up>& __x)
1327 return static_cast<bool>(__x) ? __v > *__x : true;
1330 template <class _Tp, class _Up>
1331 _LIBCPP_INLINE_VISIBILITY constexpr
1333 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
1334 _VSTD::declval<const _Up&>()), bool>,
1337 operator>=(const optional<_Tp>& __x, const _Up& __v)
1339 return static_cast<bool>(__x) ? *__x >= __v : false;
1342 template <class _Tp, class _Up>
1343 _LIBCPP_INLINE_VISIBILITY constexpr
1345 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
1346 _VSTD::declval<const _Up&>()), bool>,
1349 operator>=(const _Tp& __v, const optional<_Up>& __x)
1351 return static_cast<bool>(__x) ? __v >= *__x : true;
1355 template <class _Tp>
1356 inline _LIBCPP_INLINE_VISIBILITY
1358 is_move_constructible_v<_Tp> && is_swappable_v<_Tp>,
1361 swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y)))
1366 template <class _Tp>
1367 _LIBCPP_INLINE_VISIBILITY constexpr
1368 optional<decay_t<_Tp>> make_optional(_Tp&& __v)
1370 return optional<decay_t<_Tp>>(_VSTD::forward<_Tp>(__v));
1373 template <class _Tp, class... _Args>
1374 _LIBCPP_INLINE_VISIBILITY constexpr
1375 optional<_Tp> make_optional(_Args&&... __args)
1377 return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...);
1380 template <class _Tp, class _Up, class... _Args>
1381 _LIBCPP_INLINE_VISIBILITY constexpr
1382 optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args)
1384 return optional<_Tp>(in_place, __il, _VSTD::forward<_Args>(__args)...);
1387 template <class _Tp>
1388 struct _LIBCPP_TEMPLATE_VIS hash<
1389 __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>>
1392 typedef optional<_Tp> argument_type;
1393 typedef size_t result_type;
1395 _LIBCPP_INLINE_VISIBILITY
1396 result_type operator()(const argument_type& __opt) const
1398 return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
1402 _LIBCPP_END_NAMESPACE_STD
1404 #endif // _LIBCPP_STD_VER > 14
1408 #endif // _LIBCPP_OPTIONAL