30 #ifndef _GLIBCXX_EXPERIMENTAL_OPTIONAL
31 #define _GLIBCXX_EXPERIMENTAL_OPTIONAL 1
33 #if __cplusplus >= 201402L
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 namespace experimental
51 inline namespace fundamentals_v1
64 #define __cpp_lib_experimental_optional 201411
69 template<
typename _Tp>
88 enum class _Construct { _Token };
91 explicit constexpr
nullopt_t(_Construct) { }
118 [[noreturn]]
inline void
119 __throw_bad_optional_access(
const char* __s)
134 template<
typename _Tp,
bool _ShouldProvideDestructor =
151 constexpr _Optional_base(nullopt_t) noexcept
152 : _Optional_base{} { }
155 template<
typename... _Args>
156 constexpr
explicit _Optional_base(in_place_t, _Args&&... __args)
157 : _M_payload(std::
forward<_Args>(__args)...), _M_engaged(true) { }
159 template<
typename _Up,
typename... _Args,
161 initializer_list<_Up>&,
164 constexpr
explicit _Optional_base(in_place_t,
165 initializer_list<_Up> __il,
167 : _M_payload(__il, std::
forward<_Args>(__args)...),
171 _Optional_base(
const _Optional_base& __other)
173 if (__other._M_engaged)
174 this->_M_construct(__other._M_get());
177 _Optional_base(_Optional_base&& __other)
178 noexcept(is_nothrow_move_constructible<_Tp>())
180 if (__other._M_engaged)
181 this->_M_construct(
std::move(__other._M_get()));
186 operator=(
const _Optional_base& __other)
188 if (this->_M_engaged && __other._M_engaged)
189 this->_M_get() = __other._M_get();
192 if (__other._M_engaged)
193 this->_M_construct(__other._M_get());
202 operator=(_Optional_base&& __other)
203 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
204 is_nothrow_move_assignable<_Tp>>())
206 if (this->_M_engaged && __other._M_engaged)
207 this->_M_get() =
std::move(__other._M_get());
210 if (__other._M_engaged)
211 this->_M_construct(
std::move(__other._M_get()));
221 if (this->_M_engaged)
222 this->_M_payload.~_Stored_type();
228 constexpr
bool _M_is_engaged() const noexcept
229 {
return this->_M_engaged; }
234 {
return _M_payload; }
237 _M_get() const noexcept
238 {
return _M_payload; }
242 template<
typename... _Args>
244 _M_construct(_Args&&... __args)
245 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
248 _Stored_type(std::
forward<_Args>(__args)...);
249 this->_M_engaged = true;
255 this->_M_engaged =
false;
256 this->_M_payload.~_Stored_type();
263 if (this->_M_engaged)
268 struct _Empty_byte { };
270 _Empty_byte _M_empty;
271 _Stored_type _M_payload;
273 bool _M_engaged =
false;
278 template<
typename _Tp>
279 class _Optional_base<_Tp, false>
282 using _Stored_type = remove_const_t<_Tp>;
285 constexpr _Optional_base() noexcept
288 constexpr _Optional_base(nullopt_t) noexcept
289 : _Optional_base{} { }
291 template<
typename... _Args>
292 constexpr
explicit _Optional_base(in_place_t, _Args&&... __args)
293 : _M_payload(std::
forward<_Args>(__args)...), _M_engaged(true) { }
295 template<
typename _Up,
typename... _Args,
297 initializer_list<_Up>&,
300 constexpr
explicit _Optional_base(in_place_t,
301 initializer_list<_Up> __il,
303 : _M_payload(__il, std::
forward<_Args>(__args)...),
306 _Optional_base(
const _Optional_base& __other)
308 if (__other._M_engaged)
309 this->_M_construct(__other._M_get());
312 _Optional_base(_Optional_base&& __other)
313 noexcept(is_nothrow_move_constructible<_Tp>())
315 if (__other._M_engaged)
316 this->_M_construct(
std::move(__other._M_get()));
320 operator=(
const _Optional_base& __other)
322 if (this->_M_engaged && __other._M_engaged)
323 this->_M_get() = __other._M_get();
326 if (__other._M_engaged)
327 this->_M_construct(__other._M_get());
335 operator=(_Optional_base&& __other)
336 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
337 is_nothrow_move_assignable<_Tp>>())
339 if (this->_M_engaged && __other._M_engaged)
340 this->_M_get() =
std::move(__other._M_get());
343 if (__other._M_engaged)
344 this->_M_construct(
std::move(__other._M_get()));
355 constexpr
bool _M_is_engaged() const noexcept
356 {
return this->_M_engaged; }
360 {
return _M_payload; }
363 _M_get() const noexcept
364 {
return _M_payload; }
366 template<
typename... _Args>
368 _M_construct(_Args&&... __args)
369 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
372 _Stored_type(std::
forward<_Args>(__args)...);
373 this->_M_engaged = true;
379 this->_M_engaged =
false;
380 this->_M_payload.~_Stored_type();
386 if (this->_M_engaged)
391 struct _Empty_byte { };
394 _Empty_byte _M_empty;
395 _Stored_type _M_payload;
397 bool _M_engaged =
false;
400 template<
typename _Tp,
typename _Up>
401 using __converts_from_optional =
402 __or_<is_constructible<_Tp, const optional<_Up>&>,
403 is_constructible<_Tp, optional<_Up>&>,
404 is_constructible<_Tp, const optional<_Up>&&>,
405 is_constructible<_Tp, optional<_Up>&&>,
406 is_convertible<const optional<_Up>&, _Tp>,
407 is_convertible<optional<_Up>&, _Tp>,
408 is_convertible<const optional<_Up>&&, _Tp>,
409 is_convertible<optional<_Up>&&, _Tp>>;
411 template<
typename _Tp,
typename _Up>
412 using __assigns_from_optional =
413 __or_<is_assignable<_Tp&, const optional<_Up>&>,
414 is_assignable<_Tp&, optional<_Up>&>,
415 is_assignable<_Tp&, const optional<_Up>&&>,
416 is_assignable<_Tp&, optional<_Up>&&>>;
423 template<
typename _Tp>
425 :
private _Optional_base<_Tp>,
426 private _Enable_copy_move<
428 is_copy_constructible<_Tp>::value,
430 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
432 is_move_constructible<_Tp>::value,
434 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
438 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
439 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
440 __not_<is_reference<_Tp>>>(),
441 "Invalid instantiation of optional<T>");
444 using _Base = _Optional_base<_Tp>;
447 using value_type = _Tp;
452 constexpr optional() =
default;
454 template <
typename _Up = _Tp,
456 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
457 is_constructible<_Tp, _Up&&>,
458 is_convertible<_Up&&, _Tp>
459 >::value,
bool> =
true>
460 constexpr optional(_Up&& __t)
461 : _Base(in_place, std::
forward<_Up>(__t)) { }
463 template <
typename _Up = _Tp,
465 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
466 is_constructible<_Tp, _Up&&>,
467 __not_<is_convertible<_Up&&, _Tp>>
468 >::value,
bool> =
false>
469 explicit constexpr optional(_Up&& __t)
470 : _Base(in_place, std::
forward<_Up>(__t)) { }
472 template <
typename _Up,
474 __not_<is_same<_Tp, _Up>>,
475 is_constructible<_Tp, const _Up&>,
476 is_convertible<const _Up&, _Tp>,
477 __not_<__converts_from_optional<_Tp, _Up>>
478 >::value,
bool> =
true>
479 constexpr optional(
const optional<_Up>& __t)
485 template <
typename _Up,
487 __not_<is_same<_Tp, _Up>>,
488 is_constructible<_Tp, const _Up&>,
489 __not_<is_convertible<const _Up&, _Tp>>,
490 __not_<__converts_from_optional<_Tp, _Up>>
491 >::value,
bool> =
false>
492 explicit constexpr optional(
const optional<_Up>& __t)
498 template <
typename _Up,
500 __not_<is_same<_Tp, _Up>>,
501 is_constructible<_Tp, _Up&&>,
502 is_convertible<_Up&&, _Tp>,
503 __not_<__converts_from_optional<_Tp, _Up>>
504 >::value,
bool> =
true>
505 constexpr optional(optional<_Up>&& __t)
511 template <
typename _Up,
513 __not_<is_same<_Tp, _Up>>,
514 is_constructible<_Tp, _Up&&>,
515 __not_<is_convertible<_Up&&, _Tp>>,
516 __not_<__converts_from_optional<_Tp, _Up>>
517 >::value,
bool> =
false>
518 explicit constexpr optional(optional<_Up>&& __t)
526 operator=(nullopt_t) noexcept
532 template<
typename _Up = _Tp>
534 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
535 is_constructible<_Tp, _Up>,
536 __not_<__and_<is_scalar<_Tp>,
537 is_same<_Tp, decay_t<_Up>>>>,
538 is_assignable<_Tp&, _Up>>::value,
542 if (this->_M_is_engaged())
543 this->_M_get() = std::forward<_Up>(__u);
545 this->_M_construct(std::forward<_Up>(__u));
550 template<
typename _Up>
552 __not_<is_same<_Tp, _Up>>,
553 is_constructible<_Tp, const _Up&>,
554 is_assignable<_Tp&, _Up>,
555 __not_<__converts_from_optional<_Tp, _Up>>,
556 __not_<__assigns_from_optional<_Tp, _Up>>
559 operator=(
const optional<_Up>& __u)
563 if (this->_M_is_engaged())
564 this->_M_get() = *__u;
566 this->_M_construct(*__u);
575 template<
typename _Up>
577 __not_<is_same<_Tp, _Up>>,
578 is_constructible<_Tp, _Up>,
579 is_assignable<_Tp&, _Up>,
580 __not_<__converts_from_optional<_Tp, _Up>>,
581 __not_<__assigns_from_optional<_Tp, _Up>>
584 operator=(optional<_Up>&& __u)
588 if (this->_M_is_engaged())
601 template<
typename... _Args>
602 enable_if_t<is_constructible<_Tp, _Args&&...>::value>
603 emplace(_Args&&... __args)
606 this->_M_construct(std::forward<_Args>(__args)...);
609 template<
typename _Up,
typename... _Args>
610 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
612 emplace(initializer_list<_Up> __il, _Args&&... __args)
615 this->_M_construct(__il, std::forward<_Args>(__args)...);
622 swap(optional& __other)
623 noexcept(is_nothrow_move_constructible<_Tp>()
624 && __is_nothrow_swappable<_Tp>::value)
628 if (this->_M_is_engaged() && __other._M_is_engaged())
629 swap(this->_M_get(), __other._M_get());
630 else if (this->_M_is_engaged())
632 __other._M_construct(
std::move(this->_M_get()));
635 else if (__other._M_is_engaged())
637 this->_M_construct(
std::move(__other._M_get()));
638 __other._M_destruct();
653 {
return this->_M_get(); }
657 {
return this->_M_get(); }
663 constexpr
const _Tp&&
667 constexpr
explicit operator bool() const noexcept
668 {
return this->_M_is_engaged(); }
673 if (this->_M_is_engaged())
674 return this->_M_get();
675 __throw_bad_optional_access(
"Attempt to access value of a "
676 "disengaged optional object");
682 if (this->_M_is_engaged())
683 return this->_M_get();
684 __throw_bad_optional_access(
"Attempt to access value of a "
685 "disengaged optional object");
691 if (this->_M_is_engaged())
693 __throw_bad_optional_access(
"Attempt to access value of a "
694 "disengaged optional object");
697 constexpr
const _Tp&&
700 if (this->_M_is_engaged())
702 __throw_bad_optional_access(
"Attempt to access value of a "
703 "disengaged optional object");
706 template<
typename _Up>
708 value_or(_Up&& __u) const&
710 static_assert(__and_<is_copy_constructible<_Tp>,
711 is_convertible<_Up&&, _Tp>>(),
712 "Cannot return value");
714 if (this->_M_is_engaged())
715 return this->_M_get();
717 return static_cast<_Tp
>(std::forward<_Up>(__u));
720 template<
typename _Up>
722 value_or(_Up&& __u) &&
724 static_assert(__and_<is_move_constructible<_Tp>,
725 is_convertible<_Up&&, _Tp>>(),
726 "Cannot return value" );
728 if (this->_M_is_engaged())
731 return static_cast<_Tp
>(std::forward<_Up>(__u));
738 template<
typename _Tp>
740 operator==(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
742 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
743 && (!__lhs || *__lhs == *__rhs);
746 template<
typename _Tp>
748 operator!=(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
749 {
return !(__lhs == __rhs); }
751 template<
typename _Tp>
753 operator<(const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
755 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
758 template<
typename _Tp>
760 operator>(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
761 {
return __rhs < __lhs; }
763 template<
typename _Tp>
765 operator<=(const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
766 {
return !(__rhs < __lhs); }
768 template<
typename _Tp>
770 operator>=(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
771 {
return !(__lhs < __rhs); }
774 template<
typename _Tp>
776 operator==(
const optional<_Tp>& __lhs, nullopt_t) noexcept
779 template<
typename _Tp>
781 operator==(nullopt_t,
const optional<_Tp>& __rhs) noexcept
784 template<
typename _Tp>
786 operator!=(
const optional<_Tp>& __lhs, nullopt_t) noexcept
787 {
return static_cast<bool>(__lhs); }
789 template<
typename _Tp>
791 operator!=(nullopt_t,
const optional<_Tp>& __rhs) noexcept
792 {
return static_cast<bool>(__rhs); }
794 template<
typename _Tp>
796 operator<(const optional<_Tp>& , nullopt_t) noexcept
799 template<
typename _Tp>
801 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
802 {
return static_cast<bool>(__rhs); }
804 template<
typename _Tp>
806 operator>(
const optional<_Tp>& __lhs, nullopt_t) noexcept
807 {
return static_cast<bool>(__lhs); }
809 template<
typename _Tp>
811 operator>(nullopt_t,
const optional<_Tp>& ) noexcept
814 template<
typename _Tp>
816 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
819 template<
typename _Tp>
821 operator<=(nullopt_t, const optional<_Tp>& ) noexcept
824 template<
typename _Tp>
826 operator>=(
const optional<_Tp>& , nullopt_t) noexcept
829 template<
typename _Tp>
831 operator>=(nullopt_t,
const optional<_Tp>& __rhs) noexcept
835 template<
typename _Tp>
837 operator==(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
838 {
return __lhs && *__lhs == __rhs; }
840 template<
typename _Tp>
842 operator==(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
843 {
return __rhs && __lhs == *__rhs; }
845 template<
typename _Tp>
847 operator!=(
const optional<_Tp>& __lhs, _Tp
const& __rhs)
848 {
return !__lhs || !(*__lhs == __rhs); }
850 template<
typename _Tp>
852 operator!=(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
853 {
return !__rhs || !(__lhs == *__rhs); }
855 template<
typename _Tp>
857 operator<(const optional<_Tp>& __lhs,
const _Tp& __rhs)
858 {
return !__lhs || *__lhs < __rhs; }
860 template<
typename _Tp>
862 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
863 {
return __rhs && __lhs < *__rhs; }
865 template<
typename _Tp>
867 operator>(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
868 {
return __lhs && __rhs < *__lhs; }
870 template<
typename _Tp>
872 operator>(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
873 {
return !__rhs || *__rhs < __lhs; }
875 template<
typename _Tp>
877 operator<=(const optional<_Tp>& __lhs,
const _Tp& __rhs)
878 {
return !__lhs || !(__rhs < *__lhs); }
880 template<
typename _Tp>
882 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
883 {
return __rhs && !(*__rhs < __lhs); }
885 template<
typename _Tp>
887 operator>=(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
888 {
return __lhs && !(*__lhs < __rhs); }
890 template<
typename _Tp>
892 operator>=(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
893 {
return !__rhs || !(__lhs < *__rhs); }
896 template<
typename _Tp>
898 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
899 noexcept(noexcept(__lhs.swap(__rhs)))
900 { __lhs.swap(__rhs); }
902 template<
typename _Tp>
903 constexpr optional<decay_t<_Tp>>
904 make_optional(_Tp&& __t)
905 {
return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
915 template<
typename _Tp>
918 using result_type = size_t;
919 using argument_type = experimental::optional<_Tp>;
922 operator()(
const experimental::optional<_Tp>& __t)
const
927 constexpr
size_t __magic_disengaged_hash =
static_cast<size_t>(-3333);
928 return __t ?
hash<_Tp> {}(*__t) : __magic_disengaged_hash;
932 _GLIBCXX_END_NAMESPACE_VERSION
937 #endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL
is_trivially_destructible
Class template for optional values.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Exception class thrown when a disengaged optional object is dereferenced.
Class template that provides copy/move constructors of optional.
constexpr nullopt_t nullopt
Tag to disengage optional objects.
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
One of two subclasses of exception.
typename remove_const< _Tp >::type remove_const_t
Alias template for remove_const.
Tag type for in-place construction.
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Primary class template hash.
Class template for optional values.
Tag type to disengage optional objects.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
logic_error(const string &__arg) _GLIBCXX_TXN_SAFE