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 __throw_bad_optional_access(
const char*)
119 __attribute__((__noreturn__));
123 __throw_bad_optional_access(const
char* __s)
138 template<
typename _Tp,
bool _ShouldProvideDestructor =
152 constexpr _Optional_base() noexcept
155 constexpr _Optional_base(nullopt_t) noexcept
156 : _Optional_base{} { }
159 template<
typename... _Args>
160 constexpr
explicit _Optional_base(in_place_t, _Args&&... __args)
161 : _M_payload(std::
forward<_Args>(__args)...), _M_engaged(true) { }
163 template<
typename _Up,
typename... _Args,
165 initializer_list<_Up>&,
168 constexpr
explicit _Optional_base(in_place_t,
169 initializer_list<_Up> __il,
171 : _M_payload(__il, std::
forward<_Args>(__args)...),
175 _Optional_base(
const _Optional_base& __other)
177 if (__other._M_engaged)
178 this->_M_construct(__other._M_get());
181 _Optional_base(_Optional_base&& __other)
182 noexcept(is_nothrow_move_constructible<_Tp>())
184 if (__other._M_engaged)
185 this->_M_construct(
std::move(__other._M_get()));
190 operator=(
const _Optional_base& __other)
192 if (this->_M_engaged && __other._M_engaged)
193 this->_M_get() = __other._M_get();
196 if (__other._M_engaged)
197 this->_M_construct(__other._M_get());
206 operator=(_Optional_base&& __other)
207 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
208 is_nothrow_move_assignable<_Tp>>())
210 if (this->_M_engaged && __other._M_engaged)
211 this->_M_get() =
std::move(__other._M_get());
214 if (__other._M_engaged)
215 this->_M_construct(
std::move(__other._M_get()));
225 if (this->_M_engaged)
226 this->_M_payload.~_Stored_type();
232 constexpr
bool _M_is_engaged() const noexcept
233 {
return this->_M_engaged; }
238 {
return _M_payload; }
241 _M_get() const noexcept
242 {
return _M_payload; }
246 template<
typename... _Args>
248 _M_construct(_Args&&... __args)
249 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
252 _Stored_type(std::
forward<_Args>(__args)...);
253 this->_M_engaged = true;
259 this->_M_engaged =
false;
260 this->_M_payload.~_Stored_type();
267 if (this->_M_engaged)
272 struct _Empty_byte { };
274 _Empty_byte _M_empty;
275 _Stored_type _M_payload;
277 bool _M_engaged =
false;
282 template<
typename _Tp>
283 class _Optional_base<_Tp, false>
286 using _Stored_type = remove_const_t<_Tp>;
289 constexpr _Optional_base() noexcept
292 constexpr _Optional_base(nullopt_t) noexcept
293 : _Optional_base{} { }
295 template<
typename... _Args>
296 constexpr
explicit _Optional_base(in_place_t, _Args&&... __args)
297 : _M_payload(std::
forward<_Args>(__args)...), _M_engaged(true) { }
299 template<
typename _Up,
typename... _Args,
301 initializer_list<_Up>&,
304 constexpr
explicit _Optional_base(in_place_t,
305 initializer_list<_Up> __il,
307 : _M_payload(__il, std::
forward<_Args>(__args)...),
310 _Optional_base(
const _Optional_base& __other)
312 if (__other._M_engaged)
313 this->_M_construct(__other._M_get());
316 _Optional_base(_Optional_base&& __other)
317 noexcept(is_nothrow_move_constructible<_Tp>())
319 if (__other._M_engaged)
320 this->_M_construct(
std::move(__other._M_get()));
324 operator=(
const _Optional_base& __other)
326 if (this->_M_engaged && __other._M_engaged)
327 this->_M_get() = __other._M_get();
330 if (__other._M_engaged)
331 this->_M_construct(__other._M_get());
339 operator=(_Optional_base&& __other)
340 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
341 is_nothrow_move_assignable<_Tp>>())
343 if (this->_M_engaged && __other._M_engaged)
344 this->_M_get() =
std::move(__other._M_get());
347 if (__other._M_engaged)
348 this->_M_construct(
std::move(__other._M_get()));
359 constexpr
bool _M_is_engaged() const noexcept
360 {
return this->_M_engaged; }
364 {
return _M_payload; }
367 _M_get() const noexcept
368 {
return _M_payload; }
370 template<
typename... _Args>
372 _M_construct(_Args&&... __args)
373 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
376 _Stored_type(std::
forward<_Args>(__args)...);
377 this->_M_engaged = true;
383 this->_M_engaged =
false;
384 this->_M_payload.~_Stored_type();
390 if (this->_M_engaged)
395 struct _Empty_byte { };
398 _Empty_byte _M_empty;
399 _Stored_type _M_payload;
401 bool _M_engaged =
false;
404 template<
typename _Tp,
typename _Up>
405 using __converts_from_optional =
406 __or_<is_constructible<_Tp, const optional<_Up>&>,
407 is_constructible<_Tp, optional<_Up>&>,
408 is_constructible<_Tp, const optional<_Up>&&>,
409 is_constructible<_Tp, optional<_Up>&&>,
410 is_convertible<const optional<_Up>&, _Tp>,
411 is_convertible<optional<_Up>&, _Tp>,
412 is_convertible<const optional<_Up>&&, _Tp>,
413 is_convertible<optional<_Up>&&, _Tp>>;
415 template<
typename _Tp,
typename _Up>
416 using __assigns_from_optional =
417 __or_<is_assignable<_Tp&, const optional<_Up>&>,
418 is_assignable<_Tp&, optional<_Up>&>,
419 is_assignable<_Tp&, const optional<_Up>&&>,
420 is_assignable<_Tp&, optional<_Up>&&>>;
427 template<
typename _Tp>
429 :
private _Optional_base<_Tp>,
430 private _Enable_copy_move<
432 is_copy_constructible<_Tp>::value,
434 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
436 is_move_constructible<_Tp>::value,
438 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
442 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
443 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
444 __not_<is_reference<_Tp>>>(),
445 "Invalid instantiation of optional<T>");
448 using _Base = _Optional_base<_Tp>;
451 using value_type = _Tp;
456 constexpr optional() =
default;
458 template <
typename _Up = _Tp,
460 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
461 is_constructible<_Tp, _Up&&>,
462 is_convertible<_Up&&, _Tp>
463 >::value,
bool> =
true>
464 constexpr optional(_Up&& __t)
467 template <
typename _Up = _Tp,
469 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
470 is_constructible<_Tp, _Up&&>,
471 __not_<is_convertible<_Up&&, _Tp>>
472 >::value,
bool> =
false>
473 explicit constexpr optional(_Up&& __t)
476 template <
typename _Up,
478 __not_<is_same<_Tp, _Up>>,
479 is_constructible<_Tp, const _Up&>,
480 is_convertible<const _Up&, _Tp>,
481 __not_<__converts_from_optional<_Tp, _Up>>
482 >::value,
bool> =
true>
483 constexpr optional(
const optional<_Up>& __t)
489 template <
typename _Up,
491 __not_<is_same<_Tp, _Up>>,
492 is_constructible<_Tp, const _Up&>,
493 __not_<is_convertible<const _Up&, _Tp>>,
494 __not_<__converts_from_optional<_Tp, _Up>>
495 >::value,
bool> =
false>
496 explicit constexpr optional(
const optional<_Up>& __t)
502 template <
typename _Up,
504 __not_<is_same<_Tp, _Up>>,
505 is_constructible<_Tp, _Up&&>,
506 is_convertible<_Up&&, _Tp>,
507 __not_<__converts_from_optional<_Tp, _Up>>
508 >::value,
bool> =
true>
509 constexpr optional(optional<_Up>&& __t)
515 template <
typename _Up,
517 __not_<is_same<_Tp, _Up>>,
518 is_constructible<_Tp, _Up&&>,
519 __not_<is_convertible<_Up&&, _Tp>>,
520 __not_<__converts_from_optional<_Tp, _Up>>
521 >::value,
bool> =
false>
522 explicit constexpr optional(optional<_Up>&& __t)
530 operator=(nullopt_t) noexcept
536 template<
typename _Up = _Tp>
538 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
539 is_constructible<_Tp, _Up>,
540 __not_<__and_<is_scalar<_Tp>,
541 is_same<_Tp, decay_t<_Up>>>>,
542 is_assignable<_Tp&, _Up>>::value,
546 if (this->_M_is_engaged())
547 this->_M_get() = std::forward<_Up>(__u);
549 this->_M_construct(std::forward<_Up>(__u));
554 template<
typename _Up>
556 __not_<is_same<_Tp, _Up>>,
557 is_constructible<_Tp, const _Up&>,
558 is_assignable<_Tp&, _Up>,
559 __not_<__converts_from_optional<_Tp, _Up>>,
560 __not_<__assigns_from_optional<_Tp, _Up>>
563 operator=(
const optional<_Up>& __u)
567 if (this->_M_is_engaged())
568 this->_M_get() = *__u;
570 this->_M_construct(*__u);
579 template<
typename _Up>
581 __not_<is_same<_Tp, _Up>>,
582 is_constructible<_Tp, _Up>,
583 is_assignable<_Tp&, _Up>,
584 __not_<__converts_from_optional<_Tp, _Up>>,
585 __not_<__assigns_from_optional<_Tp, _Up>>
588 operator=(optional<_Up>&& __u)
592 if (this->_M_is_engaged())
605 template<
typename... _Args>
606 enable_if_t<is_constructible<_Tp, _Args&&...>::value>
607 emplace(_Args&&... __args)
610 this->_M_construct(std::forward<_Args>(__args)...);
613 template<
typename _Up,
typename... _Args>
614 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
616 emplace(initializer_list<_Up> __il, _Args&&... __args)
619 this->_M_construct(__il, std::forward<_Args>(__args)...);
626 swap(optional& __other)
627 noexcept(is_nothrow_move_constructible<_Tp>()
628 && __is_nothrow_swappable<_Tp>::value)
632 if (this->_M_is_engaged() && __other._M_is_engaged())
633 swap(this->_M_get(), __other._M_get());
634 else if (this->_M_is_engaged())
636 __other._M_construct(
std::move(this->_M_get()));
639 else if (__other._M_is_engaged())
641 this->_M_construct(
std::move(__other._M_get()));
642 __other._M_destruct();
657 {
return this->_M_get(); }
661 {
return this->_M_get(); }
667 constexpr
const _Tp&&
671 constexpr
explicit operator bool() const noexcept
672 {
return this->_M_is_engaged(); }
677 return this->_M_is_engaged()
679 : (__throw_bad_optional_access(
"Attempt to access value of a "
680 "disengaged optional object"),
687 return this->_M_is_engaged()
689 : (__throw_bad_optional_access(
"Attempt to access value of a "
690 "disengaged optional object"),
697 return this->_M_is_engaged()
699 : (__throw_bad_optional_access(
"Attempt to access value of a "
700 "disengaged optional object"),
701 std::
move(this->_M_get()));
704 constexpr
const _Tp&&
707 return this->_M_is_engaged()
709 : (__throw_bad_optional_access(
"Attempt to access value of a "
710 "disengaged optional object"),
711 std::
move(this->_M_get()));
714 template<
typename _Up>
716 value_or(_Up&& __u) const&
718 static_assert(__and_<is_copy_constructible<_Tp>,
719 is_convertible<_Up&&, _Tp>>(),
720 "Cannot return value");
722 return this->_M_is_engaged()
724 : static_cast<_Tp>(std::
forward<_Up>(__u));
727 template<
typename _Up>
729 value_or(_Up&& __u) &&
731 static_assert(__and_<is_move_constructible<_Tp>,
732 is_convertible<_Up&&, _Tp>>(),
733 "Cannot return value" );
735 return this->_M_is_engaged()
737 : static_cast<_Tp>(std::
forward<_Up>(__u));
744 template<
typename _Tp>
746 operator==(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
748 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
749 && (!__lhs || *__lhs == *__rhs);
752 template<
typename _Tp>
754 operator!=(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
755 {
return !(__lhs == __rhs); }
757 template<
typename _Tp>
759 operator<(const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
761 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
764 template<
typename _Tp>
766 operator>(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
767 {
return __rhs < __lhs; }
769 template<
typename _Tp>
771 operator<=(const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
772 {
return !(__rhs < __lhs); }
774 template<
typename _Tp>
776 operator>=(
const optional<_Tp>& __lhs,
const optional<_Tp>& __rhs)
777 {
return !(__lhs < __rhs); }
780 template<
typename _Tp>
782 operator==(
const optional<_Tp>& __lhs, nullopt_t) noexcept
785 template<
typename _Tp>
787 operator==(nullopt_t,
const optional<_Tp>& __rhs) noexcept
790 template<
typename _Tp>
792 operator!=(
const optional<_Tp>& __lhs, nullopt_t) noexcept
793 {
return static_cast<bool>(__lhs); }
795 template<
typename _Tp>
797 operator!=(nullopt_t,
const optional<_Tp>& __rhs) noexcept
798 {
return static_cast<bool>(__rhs); }
800 template<
typename _Tp>
802 operator<(const optional<_Tp>& , nullopt_t) noexcept
805 template<
typename _Tp>
807 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
808 {
return static_cast<bool>(__rhs); }
810 template<
typename _Tp>
812 operator>(
const optional<_Tp>& __lhs, nullopt_t) noexcept
813 {
return static_cast<bool>(__lhs); }
815 template<
typename _Tp>
817 operator>(nullopt_t,
const optional<_Tp>& ) noexcept
820 template<
typename _Tp>
822 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
825 template<
typename _Tp>
827 operator<=(nullopt_t, const optional<_Tp>& ) noexcept
830 template<
typename _Tp>
832 operator>=(
const optional<_Tp>& , nullopt_t) noexcept
835 template<
typename _Tp>
837 operator>=(nullopt_t,
const optional<_Tp>& __rhs) noexcept
841 template<
typename _Tp>
843 operator==(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
844 {
return __lhs && *__lhs == __rhs; }
846 template<
typename _Tp>
848 operator==(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
849 {
return __rhs && __lhs == *__rhs; }
851 template<
typename _Tp>
853 operator!=(
const optional<_Tp>& __lhs, _Tp
const& __rhs)
854 {
return !__lhs || !(*__lhs == __rhs); }
856 template<
typename _Tp>
858 operator!=(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
859 {
return !__rhs || !(__lhs == *__rhs); }
861 template<
typename _Tp>
863 operator<(const optional<_Tp>& __lhs,
const _Tp& __rhs)
864 {
return !__lhs || *__lhs < __rhs; }
866 template<
typename _Tp>
868 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
869 {
return __rhs && __lhs < *__rhs; }
871 template<
typename _Tp>
873 operator>(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
874 {
return __lhs && __rhs < *__lhs; }
876 template<
typename _Tp>
878 operator>(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
879 {
return !__rhs || *__rhs < __lhs; }
881 template<
typename _Tp>
883 operator<=(const optional<_Tp>& __lhs,
const _Tp& __rhs)
884 {
return !__lhs || !(__rhs < *__lhs); }
886 template<
typename _Tp>
888 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
889 {
return __rhs && !(*__rhs < __lhs); }
891 template<
typename _Tp>
893 operator>=(
const optional<_Tp>& __lhs,
const _Tp& __rhs)
894 {
return __lhs && !(*__lhs < __rhs); }
896 template<
typename _Tp>
898 operator>=(
const _Tp& __lhs,
const optional<_Tp>& __rhs)
899 {
return !__rhs || !(__lhs < *__rhs); }
902 template<
typename _Tp>
904 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
905 noexcept(noexcept(__lhs.swap(__rhs)))
906 { __lhs.swap(__rhs); }
908 template<
typename _Tp>
909 constexpr optional<decay_t<_Tp>>
910 make_optional(_Tp&& __t)
911 {
return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
921 template<
typename _Tp>
922 struct hash<experimental::optional<_Tp>>
924 using result_type = size_t;
925 using argument_type = experimental::optional<_Tp>;
928 operator()(
const experimental::optional<_Tp>& __t)
const
933 constexpr
size_t __magic_disengaged_hash =
static_cast<size_t>(-3333);
934 return __t ?
hash<_Tp> {}(*__t) : __magic_disengaged_hash;
938 _GLIBCXX_END_NAMESPACE_VERSION
943 #endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL
Tag type for in-place construction.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Class template for optional values.
constexpr in_place_t in_place
Tag for in-place construction.
logic_error(const string &__arg) _GLIBCXX_TXN_SAFE
Exception class thrown when a disengaged optional object is dereferenced.
One of two subclasses of exception.
constexpr nullopt_t nullopt
Tag to disengage optional objects.
Tag type to disengage optional objects.
is_trivially_destructible
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Primary class template hash.
typename remove_const< _Tp >::type remove_const_t
Alias template for remove_const.
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.