29 #ifndef _GLIBCXX_EXPERIMENTAL_OPTIONAL
30 #define _GLIBCXX_EXPERIMENTAL_OPTIONAL 1
44 #if __cplusplus <= 201103L
57 namespace std _GLIBCXX_VISIBILITY(default)
59 namespace experimental
61 inline namespace fundamentals_v1
63 _GLIBCXX_BEGIN_NAMESPACE_VERSION
76 #define __cpp_lib_experimental_optional 201411
81 template<
typename _Tp>
100 enum class _Construct { _Token };
103 explicit constexpr
nullopt_t(_Construct) { }
128 __throw_bad_optional_access(
const char*)
129 __attribute__((__noreturn__));
133 __throw_bad_optional_access(const
char* __s)
136 template<
typename _Tp,
typename =
void>
139 template<
typename _Tp>
140 struct _Has_addressof_mem<_Tp,
141 __void_t<decltype( std::declval<const _Tp&>().operator&() )>
145 template<
typename _Tp,
typename =
void>
148 template<
typename _Tp>
149 struct _Has_addressof_free<_Tp,
150 __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
161 template<
typename _Tp>
163 : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
172 template<typename _Tp, enable_if_t<!_Has_addressof<_Tp>::value,
int>...>
179 template<typename _Tp, enable_if_t<_Has_addressof<_Tp>::value,
int>...>
195 template<
typename _Tp,
bool _ShouldProvideDestructor =
196 !is_trivially_destructible<_Tp>::value>
203 using _Stored_type = remove_const_t<_Tp>;
217 : _M_payload(__t), _M_engaged(
true) { }
219 constexpr _Optional_base(_Tp&& __t)
220 : _M_payload(std::move(__t)), _M_engaged(
true) { }
222 template<
typename... _Args>
223 constexpr
explicit _Optional_base(
in_place_t, _Args&&... __args)
224 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(
true) { }
226 template<
typename _Up,
typename... _Args,
227 enable_if_t<is_constructible<_Tp,
234 : _M_payload(__il, std::forward<_Args>(__args)...),
238 _Optional_base(
const _Optional_base& __other)
240 if (__other._M_engaged)
241 this->_M_construct(__other._M_get());
244 _Optional_base(_Optional_base&& __other)
245 noexcept(is_nothrow_move_constructible<_Tp>())
247 if (__other._M_engaged)
248 this->_M_construct(std::move(__other._M_get()));
253 operator=(
const _Optional_base& __other)
255 if (this->_M_engaged && __other._M_engaged)
256 this->_M_get() = __other._M_get();
259 if (__other._M_engaged)
260 this->_M_construct(__other._M_get());
269 operator=(_Optional_base&& __other)
270 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
271 is_nothrow_move_assignable<_Tp>>())
273 if (this->_M_engaged && __other._M_engaged)
274 this->_M_get() = std::move(__other._M_get());
277 if (__other._M_engaged)
278 this->_M_construct(std::move(__other._M_get()));
288 if (this->_M_engaged)
289 this->_M_payload.~_Stored_type();
295 constexpr
bool _M_is_engaged()
const noexcept
296 {
return this->_M_engaged; }
301 {
return _M_payload; }
304 _M_get()
const noexcept
305 {
return _M_payload; }
309 template<
typename... _Args>
311 _M_construct(_Args&&... __args)
312 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
315 _Stored_type(std::forward<_Args>(__args)...);
316 this->_M_engaged =
true;
322 this->_M_engaged =
false;
323 this->_M_payload.~_Stored_type();
330 if (this->_M_engaged)
335 struct _Empty_byte { };
337 _Empty_byte _M_empty;
338 _Stored_type _M_payload;
340 bool _M_engaged =
false;
345 template<
typename _Tp>
349 using _Stored_type = remove_const_t<_Tp>;
359 : _M_payload(__t), _M_engaged(
true) { }
361 constexpr _Optional_base(_Tp&& __t)
362 : _M_payload(std::move(__t)), _M_engaged(
true) { }
364 template<
typename... _Args>
365 constexpr
explicit _Optional_base(
in_place_t, _Args&&... __args)
366 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(
true) { }
368 template<
typename _Up,
typename... _Args,
369 enable_if_t<is_constructible<_Tp,
376 : _M_payload(__il, std::forward<_Args>(__args)...),
379 _Optional_base(
const _Optional_base& __other)
381 if (__other._M_engaged)
382 this->_M_construct(__other._M_get());
385 _Optional_base(_Optional_base&& __other)
386 noexcept(is_nothrow_move_constructible<_Tp>())
388 if (__other._M_engaged)
389 this->_M_construct(std::move(__other._M_get()));
393 operator=(
const _Optional_base& __other)
395 if (this->_M_engaged && __other._M_engaged)
396 this->_M_get() = __other._M_get();
399 if (__other._M_engaged)
400 this->_M_construct(__other._M_get());
408 operator=(_Optional_base&& __other)
409 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
410 is_nothrow_move_assignable<_Tp>>())
412 if (this->_M_engaged && __other._M_engaged)
413 this->_M_get() = std::move(__other._M_get());
416 if (__other._M_engaged)
417 this->_M_construct(std::move(__other._M_get()));
428 constexpr
bool _M_is_engaged()
const noexcept
429 {
return this->_M_engaged; }
433 {
return _M_payload; }
436 _M_get()
const noexcept
437 {
return _M_payload; }
439 template<
typename... _Args>
441 _M_construct(_Args&&... __args)
442 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
445 _Stored_type(std::forward<_Args>(__args)...);
446 this->_M_engaged =
true;
452 this->_M_engaged =
false;
453 this->_M_payload.~_Stored_type();
459 if (this->_M_engaged)
464 struct _Empty_byte { };
467 _Empty_byte _M_empty;
468 _Stored_type _M_payload;
470 bool _M_engaged =
false;
473 template<
typename _Tp>
480 template<
typename _Tp>
481 struct __is_optional_impl<optional<_Tp>> :
true_type
484 template<
typename _Tp>
486 :
public __is_optional_impl<std::remove_cv_t<std::remove_reference_t<_Tp>>>
493 template<
typename _Tp>
495 :
private _Optional_base<_Tp>,
496 private _Enable_copy_move<
498 is_copy_constructible<_Tp>::value,
500 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
502 is_move_constructible<_Tp>::value,
504 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
508 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
509 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
510 __not_<is_reference<_Tp>>>(),
511 "Invalid instantiation of optional<T>");
514 using _Base = _Optional_base<_Tp>;
517 using value_type = _Tp;
522 constexpr optional() =
default;
524 template <
typename _Up,
526 __not_<is_same<_Tp, _Up>>,
527 is_constructible<_Tp, _Up&&>,
528 is_convertible<_Up&&, _Tp>
529 >::value,
bool> =
true>
530 constexpr optional(_Up&& __t)
531 : _Base(_Tp(std::
forward<_Up>(__t))) { }
533 template <
typename _Up,
535 __not_<is_same<_Tp, _Up>>,
536 is_constructible<_Tp, _Up&&>,
537 __not_<is_convertible<_Up&&, _Tp>>
538 >::value,
bool> =
false>
539 explicit constexpr optional(_Up&& __t)
540 : _Base(_Tp(std::
forward<_Up>(__t))) { }
542 template <
typename _Up,
544 __not_<is_same<_Tp, _Up>>,
545 __not_<is_constructible<
546 _Tp,
const optional<_Up>&>>,
547 __not_<is_convertible<
548 const optional<_Up>&, _Tp>>,
549 is_constructible<_Tp, const _Up&>,
550 is_convertible<const _Up&, _Tp>
551 >::value,
bool> =
true>
552 constexpr optional(
const optional<_Up>& __t)
553 : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
555 template <
typename _Up,
557 __not_<is_same<_Tp, _Up>>,
558 __not_<is_constructible<
559 _Tp,
const optional<_Up>&>>,
560 __not_<is_convertible<
561 const optional<_Up>&, _Tp>>,
562 is_constructible<_Tp, const _Up&>,
563 __not_<is_convertible<const _Up&, _Tp>>
564 >::value,
bool> =
false>
565 explicit constexpr optional(
const optional<_Up>& __t)
566 : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
568 template <
typename _Up,
570 __not_<is_same<_Tp, _Up>>,
571 __not_<is_constructible<
572 _Tp, optional<_Up>&&>>,
573 __not_<is_convertible<
574 optional<_Up>&&, _Tp>>,
575 is_constructible<_Tp, _Up&&>,
576 is_convertible<_Up&&, _Tp>
577 >::value,
bool> =
true>
578 constexpr optional(optional<_Up>&& __t)
579 : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
581 template <
typename _Up,
583 __not_<is_same<_Tp, _Up>>,
584 __not_<is_constructible<
585 _Tp, optional<_Up>&&>>,
586 __not_<is_convertible<
587 optional<_Up>&&, _Tp>>,
588 is_constructible<_Tp, _Up&&>,
589 __not_<is_convertible<_Up&&, _Tp>>
590 >::value,
bool> =
false>
591 explicit constexpr optional(optional<_Up>&& __t)
592 : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
596 operator=(nullopt_t) noexcept
602 template<
typename _Up,
604 __not_<is_same<_Up, nullopt_t>>,
605 __not_<__is_optional<_Up>>>::value,
610 static_assert(__and_<is_constructible<_Tp, _Up>,
611 is_assignable<_Tp&, _Up>>(),
612 "Cannot assign to value type from argument");
614 if (this->_M_is_engaged())
615 this->_M_get() = std::forward<_Up>(__u);
617 this->_M_construct(std::forward<_Up>(__u));
622 template<
typename _Up,
624 __not_<is_same<_Tp, _Up>>>::value,
627 operator=(
const optional<_Up>& __u)
629 static_assert(__and_<is_constructible<_Tp, _Up>,
630 is_assignable<_Tp&, _Up>>(),
631 "Cannot assign to value type from argument");
635 if (this->_M_is_engaged())
636 this->_M_get() = *__u;
638 this->_M_construct(*__u);
647 template<
typename _Up,
649 __not_<is_same<_Tp, _Up>>>::value,
652 operator=(optional<_Up>&& __u)
654 static_assert(__and_<is_constructible<_Tp, _Up>,
655 is_assignable<_Tp&, _Up>>(),
656 "Cannot assign to value type from argument");
660 if (this->_M_is_engaged())
661 this->_M_get() = std::move(*__u);
663 this->_M_construct(std::move(*__u));
673 template<
typename... _Args>
675 emplace(_Args&&... __args)
677 static_assert(is_constructible<_Tp, _Args&&...>(),
678 "Cannot emplace value type from arguments");
681 this->_M_construct(std::forward<_Args>(__args)...);
684 template<
typename _Up,
typename... _Args>
685 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
687 emplace(initializer_list<_Up> __il, _Args&&... __args)
690 this->_M_construct(__il, std::forward<_Args>(__args)...);
697 swap(optional& __other)
698 noexcept(is_nothrow_move_constructible<_Tp>()
699 && noexcept(
swap(declval<_Tp&>(), declval<_Tp&>())))
703 if (this->_M_is_engaged() && __other._M_is_engaged())
704 swap(this->_M_get(), __other._M_get());
705 else if (this->_M_is_engaged())
707 __other._M_construct(std::move(this->_M_get()));
710 else if (__other._M_is_engaged())
712 this->_M_construct(std::move(__other._M_get()));
713 __other._M_destruct();
728 {
return this->_M_get(); }
732 {
return this->_M_get(); }
736 {
return std::move(this->_M_get()); }
738 constexpr
const _Tp&&
740 {
return std::move(this->_M_get()); }
742 constexpr
explicit operator bool() const noexcept
743 {
return this->_M_is_engaged(); }
748 return this->_M_is_engaged()
750 : (__throw_bad_optional_access(
"Attempt to access value of a "
751 "disengaged optional object"),
758 return this->_M_is_engaged()
760 : (__throw_bad_optional_access(
"Attempt to access value of a "
761 "disengaged optional object"),
768 return this->_M_is_engaged()
769 ? std::move(this->_M_get())
770 : (__throw_bad_optional_access(
"Attempt to access value of a "
771 "disengaged optional object"),
772 std::move(this->_M_get()));
775 constexpr
const _Tp&&
778 return this->_M_is_engaged()
779 ? std::move(this->_M_get())
780 : (__throw_bad_optional_access(
"Attempt to access value of a "
781 "disengaged optional object"),
782 std::move(this->_M_get()));
785 template<
typename _Up>
787 value_or(_Up&& __u) const&
789 static_assert(__and_<is_copy_constructible<_Tp>,
790 is_convertible<_Up&&, _Tp>>(),
791 "Cannot return value");
793 return this->_M_is_engaged()
795 : static_cast<_Tp>(std::
forward<_Up>(__u));
798 template<
typename _Up>
800 value_or(_Up&& __u) &&
802 static_assert(__and_<is_move_constructible<_Tp>,
803 is_convertible<_Up&&, _Tp>>(),
804 "Cannot return value" );
806 return this->_M_is_engaged()
807 ? std::move(this->_M_get())
808 : static_cast<_Tp>(std::
forward<_Up>(__u));
813 template<
typename _Tp>
815 operator==(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
817 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
818 && (!__lhs || *__lhs == *__rhs);
821 template<
typename _Tp>
823 operator!=(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
824 {
return !(__lhs == __rhs); }
826 template<
typename _Tp>
828 operator<(const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
830 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
833 template<
typename _Tp>
835 operator>(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
836 {
return __rhs < __lhs; }
838 template<
typename _Tp>
840 operator<=(const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
841 {
return !(__rhs < __lhs); }
843 template<
typename _Tp>
845 operator>=(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
846 {
return !(__lhs < __rhs); }
849 template<
typename _Tp>
851 operator==(
const optional<_Tp>& __lhs, nullopt_t) noexcept
854 template<
typename _Tp>
856 operator==(nullopt_t,
const optional<_Tp>& __rhs) noexcept
859 template<
typename _Tp>
861 operator!=(
const optional<_Tp>& __lhs, nullopt_t) noexcept
862 {
return static_cast<bool>(__lhs); }
864 template<
typename _Tp>
866 operator!=(nullopt_t,
const optional<_Tp>& __rhs) noexcept
867 {
return static_cast<bool>(__rhs); }
869 template<
typename _Tp>
871 operator<(const optional<_Tp>& , nullopt_t) noexcept
874 template<
typename _Tp>
876 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
877 {
return static_cast<bool>(__rhs); }
879 template<
typename _Tp>
881 operator>(
const optional<_Tp>& __lhs, nullopt_t) noexcept
882 {
return static_cast<bool>(__lhs); }
884 template<
typename _Tp>
886 operator>(nullopt_t,
const optional<_Tp>& ) noexcept
889 template<
typename _Tp>
891 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
894 template<
typename _Tp>
896 operator<=(nullopt_t, const optional<_Tp>& ) noexcept
899 template<
typename _Tp>
901 operator>=(
const optional<_Tp>& , nullopt_t) noexcept
904 template<
typename _Tp>
906 operator>=(nullopt_t,
const optional<_Tp>& __rhs) noexcept
910 template<
typename _Tp>
912 operator==(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
913 {
return __lhs && *__lhs == __rhs; }
915 template<
typename _Tp>
917 operator==(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
918 {
return __rhs && __lhs == *__rhs; }
920 template<
typename _Tp>
922 operator!=(
const optional<_Tp>& __lhs, _Tp
const& __rhs)
923 {
return !__lhs || !(*__lhs == __rhs); }
925 template<
typename _Tp>
927 operator!=(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
928 {
return !__rhs || !(__lhs == *__rhs); }
930 template<
typename _Tp>
932 operator<(const optional<_Tp>& __lhs,
const _Tp& __rhs)
933 {
return !__lhs || *__lhs < __rhs; }
935 template<
typename _Tp>
937 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
938 {
return __rhs && __lhs < *__rhs; }
940 template<
typename _Tp>
942 operator>(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
943 {
return __lhs && __rhs < *__lhs; }
945 template<
typename _Tp>
947 operator>(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
948 {
return !__rhs || *__rhs < __lhs; }
950 template<
typename _Tp>
952 operator<=(const optional<_Tp>& __lhs,
const _Tp& __rhs)
953 {
return !__lhs || !(__rhs < *__lhs); }
955 template<
typename _Tp>
957 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
958 {
return __rhs && !(*__rhs < __lhs); }
960 template<
typename _Tp>
962 operator>=(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
963 {
return __lhs && !(*__lhs < __rhs); }
965 template<
typename _Tp>
967 operator>=(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
968 {
return !__rhs || !(__lhs < *__rhs); }
971 template<
typename _Tp>
973 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
974 noexcept(noexcept(__lhs.swap(__rhs)))
975 { __lhs.swap(__rhs); }
977 template<
typename _Tp>
978 constexpr optional<decay_t<_Tp>>
979 make_optional(_Tp&& __t)
980 {
return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
983 _GLIBCXX_END_NAMESPACE_VERSION
988 template<
typename _Tp>
989 struct hash<experimental::optional<_Tp>>
991 using result_type = size_t;
992 using argument_type = experimental::optional<_Tp>;
995 operator()(
const experimental::optional<_Tp>& __t)
const
996 noexcept(noexcept(hash<_Tp> {}(*__t)))
1000 constexpr
size_t __magic_disengaged_hash =
static_cast<size_t>(-3333);
1001 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1008 #endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL
constexpr in_place_t in_place
Tag for in-place construction.
Tag type for in-place construction.
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Trait that detects the presence of an overloaded unary operator&.
Class template for optional values.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
logic_error(const string &__arg) _GLIBCXX_TXN_SAFE
void swap(basic_filebuf< _CharT, _Traits > &__x, basic_filebuf< _CharT, _Traits > &__y)
Swap specialization for filebufs.
constexpr nullopt_t nullopt
Tag to disengage optional objects.
One of two subclasses of exception.
constexpr _Tp * __constexpr_addressof(_Tp &__t)
An overload that attempts to take the address of an lvalue as a constant expression. Falls back to __addressof in the presence of an overloaded addressof operator (unary operator&), in which case the call will not be a constant expression.
Exception class thrown when a disengaged optional object is dereferenced.
Class template that holds the necessary state for Optional values and that has the responsibility for...
Tag type to disengage optional objects.