Skip to content

Commit 5cf4603

Browse files
committed
Replace all usages of optional::value_type with _Tp, remove reference on value_type
1 parent 65e92c4 commit 5cf4603

File tree

2 files changed

+56
-58
lines changed

2 files changed

+56
-58
lines changed

libcxx/include/optional

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -687,24 +687,23 @@ class _LIBCPP_DECLSPEC_EMPTY_BASES optional
687687
using __base _LIBCPP_NODEBUG = __optional_move_assign_base<_Tp>;
688688

689689
public:
690-
using value_type = _Tp;
690+
using value_type = __libcpp_remove_reference_t<_Tp>;
691691

692692
using __trivially_relocatable _LIBCPP_NODEBUG =
693693
conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, optional, void>;
694694
using __replaceable _LIBCPP_NODEBUG = conditional_t<__is_replaceable_v<_Tp>, optional, void>;
695695

696696
private:
697-
static_assert(!is_same_v<__remove_cvref_t<value_type>, in_place_t>,
697+
static_assert(!is_same_v<__remove_cvref_t<_Tp>, in_place_t>,
698698
"instantiation of optional with in_place_t is ill-formed");
699-
static_assert(!is_same_v<__remove_cvref_t<value_type>, nullopt_t>,
700-
"instantiation of optional with nullopt_t is ill-formed");
699+
static_assert(!is_same_v<__remove_cvref_t<_Tp>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed");
701700
# if _LIBCPP_STD_VER >= 26
702701
static_assert(!is_rvalue_reference_v<_Tp>, "instantiation of optional with an rvalue reference type is ill-formed");
703702
# else
704703
static_assert(!is_reference_v<_Tp>, "instantiation of optional with a reference type is ill-formed");
705704
# endif
706-
static_assert(is_destructible_v<value_type>, "instantiation of optional with a non-destructible type is ill-formed");
707-
static_assert(!is_array_v<value_type>, "instantiation of optional with an array type is ill-formed");
705+
static_assert(is_destructible_v<_Tp>, "instantiation of optional with a non-destructible type is ill-formed");
706+
static_assert(!is_array_v<_Tp>, "instantiation of optional with an array type is ill-formed");
708707

709708
# if _LIBCPP_STD_VER >= 26
710709
template <class _Up>
@@ -785,18 +784,15 @@ public:
785784

786785
template <class _InPlaceT,
787786
class... _Args,
788-
enable_if_t<_And<_IsSame<_InPlaceT, in_place_t>, is_constructible<value_type, _Args...>>::value, int> = 0>
787+
enable_if_t<_And<_IsSame<_InPlaceT, in_place_t>, is_constructible<_Tp, _Args...>>::value, int> = 0>
789788
_LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args)
790789
: __base(in_place, std::forward<_Args>(__args)...) {}
791790

792-
template <class _Up,
793-
class... _Args,
794-
enable_if_t<is_constructible_v<value_type, initializer_list<_Up>&, _Args...>, int> = 0>
791+
template <class _Up, class... _Args, enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
795792
_LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
796793
: __base(in_place, __il, std::forward<_Args>(__args)...) {}
797794

798-
template <class _Up = value_type,
799-
enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
795+
template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
800796
_LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}
801797

802798
template <class _Up = remove_cv_t<_Tp>,
@@ -825,14 +821,11 @@ public:
825821

826822
// deleted optional<T&> constructors
827823
# if _LIBCPP_STD_VER >= 26
828-
template <class _Up,
829-
class... _Args,
830-
enable_if_t<is_constructible_v<value_type, initializer_list<_Up>&, _Args...>, int> = 0>
824+
template <class _Up, class... _Args, enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
831825
requires __libcpp_opt_ref_ctor_deleted<_Up>
832826
_LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args) = delete;
833827

834-
template <class _Up = value_type,
835-
enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
828+
template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
836829
requires __libcpp_opt_ref_ctor_deleted<_Up>
837830
_LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) = delete;
838831

@@ -876,11 +869,11 @@ public:
876869
_LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default;
877870

878871
// LWG2756
879-
template <class _Up = remove_cv_t<value_type>,
872+
template <class _Up = remove_cv_t<_Tp>,
880873
enable_if_t<_And<_IsNotSame<__remove_cvref_t<_Up>, optional>,
881-
_Or<_IsNotSame<__remove_cvref_t<_Up>, value_type>, _Not<is_scalar<value_type>>>,
882-
is_constructible<value_type, _Up>,
883-
is_assignable<value_type&, _Up>>::value,
874+
_Or<_IsNotSame<__remove_cvref_t<_Up>, _Tp>, _Not<is_scalar<_Tp>>>,
875+
is_constructible<_Tp, _Up>,
876+
is_assignable<_Tp&, _Up>>::value,
884877
int> = 0>
885878
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) {
886879
if (this->has_value())
@@ -904,7 +897,7 @@ public:
904897
return *this;
905898
}
906899

907-
template <class... _Args, enable_if_t<is_constructible_v<value_type, _Args...>, int> = 0>
900+
template <class... _Args, enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
908901
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
909902
reset();
910903
this->__construct(std::forward<_Args>(__args)...);
@@ -913,7 +906,7 @@ public:
913906

914907
template <class _Up,
915908
class... _Args,
916-
enable_if_t<is_constructible_v<value_type, initializer_list<_Up>&, _Args...>
909+
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
917910
# if _LIBCPP_STD_VER >= 26
918911
&& !reference_constructs_from_temporary_v<_Tp&, _Up>
919912
# endif
@@ -926,7 +919,7 @@ public:
926919
}
927920

928921
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
929-
swap(optional& __opt) noexcept(is_nothrow_move_constructible_v<value_type> && is_nothrow_swappable_v<value_type>) {
922+
swap(optional& __opt) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>) {
930923
if (this->has_value() == __opt.has_value()) {
931924
if (this->has_value())
932925
this->__swap(__opt);
@@ -941,32 +934,32 @@ public:
941934
}
942935
}
943936

944-
_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type const> operator->() const noexcept {
937+
_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp const> operator->() const noexcept {
945938
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
946939
return std::addressof(this->__get());
947940
}
948941

949-
_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type> operator->() noexcept {
942+
_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() noexcept {
950943
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
951944
return std::addressof(this->__get());
952945
}
953946

954-
_LIBCPP_HIDE_FROM_ABI constexpr const value_type& operator*() const& noexcept {
947+
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
955948
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
956949
return this->__get();
957950
}
958951

959-
_LIBCPP_HIDE_FROM_ABI constexpr value_type& operator*() & noexcept {
952+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
960953
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
961954
return this->__get();
962955
}
963956

964-
_LIBCPP_HIDE_FROM_ABI constexpr value_type&& operator*() && noexcept {
957+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
965958
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
966959
return std::move(this->__get());
967960
}
968961

969-
_LIBCPP_HIDE_FROM_ABI constexpr const value_type&& operator*() const&& noexcept {
962+
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
970963
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
971964
return std::move(this->__get());
972965
}
@@ -976,25 +969,25 @@ public:
976969
using __base::__get;
977970
using __base::has_value;
978971

979-
_LIBCPP_HIDE_FROM_ABI constexpr value_type const& value() const& {
972+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& {
980973
if (!this->has_value())
981974
std::__throw_bad_optional_access();
982975
return this->__get();
983976
}
984977

985-
_LIBCPP_HIDE_FROM_ABI constexpr value_type& value() & {
978+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
986979
if (!this->has_value())
987980
std::__throw_bad_optional_access();
988981
return this->__get();
989982
}
990983

991-
_LIBCPP_HIDE_FROM_ABI constexpr value_type&& value() && {
984+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
992985
if (!this->has_value())
993986
std::__throw_bad_optional_access();
994987
return std::move(this->__get());
995988
}
996989

997-
_LIBCPP_HIDE_FROM_ABI constexpr value_type const&& value() const&& {
990+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& {
998991
if (!this->has_value())
999992
std::__throw_bad_optional_access();
1000993
return std::move(this->__get());
@@ -1005,37 +998,37 @@ public:
1005998
requires(!(is_lvalue_reference_v<_Tp> && is_function_v<__libcpp_remove_reference_t<_Tp>>) &&
1006999
!(is_lvalue_reference_v<_Tp> && is_array_v<__libcpp_remove_reference_t<_Tp>>))
10071000
# endif
1008-
_LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) const& {
1009-
static_assert(is_copy_constructible_v<value_type>, "optional<T>::value_or: T must be copy constructible");
1010-
static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
1011-
return this->has_value() ? this->__get() : static_cast<value_type>(std::forward<_Up>(__v));
1001+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
1002+
static_assert(is_copy_constructible_v<_Tp>, "optional<T>::value_or: T must be copy constructible");
1003+
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
1004+
return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));
10121005
}
10131006

10141007
template <class _Up = remove_cv_t<_Tp>>
10151008
# if _LIBCPP_STD_VER >= 26
10161009
requires(!is_lvalue_reference_v<_Tp>)
10171010
# endif
1018-
_LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) && {
1019-
static_assert(is_move_constructible_v<value_type>, "optional<T>::value_or: T must be move constructible");
1020-
static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
1021-
return this->has_value() ? std::move(this->__get()) : static_cast<value_type>(std::forward<_Up>(__v));
1011+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
1012+
static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
1013+
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
1014+
return this->has_value() ? std::move(this->__get()) : static_cast<_Tp>(std::forward<_Up>(__v));
10221015
}
10231016

10241017
# if _LIBCPP_STD_VER >= 26
10251018
template <class _Up = remove_cv_t<_Tp>>
10261019
requires(is_lvalue_reference_v<_Tp> &&
10271020
!(is_function_v<__libcpp_remove_reference_t<_Tp>> || is_array_v<__libcpp_remove_reference_t<_Tp>>))
1028-
_LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) && {
1029-
static_assert(is_move_constructible_v<value_type>, "optional<T>::value_or: T must be move constructible");
1030-
static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
1031-
return this->has_value() ? this->__get() : static_cast<value_type>(std::forward<_Up>(__v));
1021+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
1022+
static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
1023+
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
1024+
return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));
10321025
}
10331026
# endif
10341027

10351028
# if _LIBCPP_STD_VER >= 23
10361029
template <class _Func>
10371030
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
1038-
using _Up = invoke_result_t<_Func, value_type&>;
1031+
using _Up = invoke_result_t<_Func, _Tp&>;
10391032
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10401033
"Result of f(value()) must be a specialization of std::optional");
10411034
if (*this)
@@ -1045,7 +1038,7 @@ public:
10451038

10461039
template <class _Func>
10471040
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
1048-
using _Up = invoke_result_t<_Func, const value_type&>;
1041+
using _Up = invoke_result_t<_Func, const _Tp&>;
10491042
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10501043
"Result of f(value()) must be a specialization of std::optional");
10511044
if (*this)
@@ -1055,7 +1048,7 @@ public:
10551048

10561049
template <class _Func>
10571050
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
1058-
using _Up = invoke_result_t<_Func, value_type&&>;
1051+
using _Up = invoke_result_t<_Func, _Tp&&>;
10591052
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10601053
"Result of f(std::move(value())) must be a specialization of std::optional");
10611054
if (*this)
@@ -1065,7 +1058,7 @@ public:
10651058

10661059
template <class _Func>
10671060
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
1068-
using _Up = invoke_result_t<_Func, const value_type&&>;
1061+
using _Up = invoke_result_t<_Func, const _Tp&&>;
10691062
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10701063
"Result of f(std::move(value())) must be a specialization of std::optional");
10711064
if (*this)
@@ -1075,7 +1068,7 @@ public:
10751068

10761069
template <class _Func>
10771070
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1078-
using _Up = remove_cv_t<invoke_result_t<_Func, value_type&>>;
1071+
using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
10791072
static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
10801073
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
10811074
static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
@@ -1087,7 +1080,7 @@ public:
10871080

10881081
template <class _Func>
10891082
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1090-
using _Up = remove_cv_t<invoke_result_t<_Func, const value_type&>>;
1083+
using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;
10911084
static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
10921085
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
10931086
static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
@@ -1099,7 +1092,7 @@ public:
10991092

11001093
template <class _Func>
11011094
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1102-
using _Up = remove_cv_t<invoke_result_t<_Func, value_type&&>>;
1095+
using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;
11031096
static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
11041097
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
11051098
static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
@@ -1111,7 +1104,7 @@ public:
11111104

11121105
template <class _Func>
11131106
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1114-
using _Up = remove_cvref_t<invoke_result_t<_Func, const value_type&&>>;
1107+
using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;
11151108
static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
11161109
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
11171110
static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
@@ -1123,7 +1116,7 @@ public:
11231116

11241117
template <invocable _Func>
11251118
_LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
1126-
requires is_copy_constructible_v<value_type>
1119+
requires is_copy_constructible_v<_Tp>
11271120
{
11281121
static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
11291122
"Result of f() should be the same type as this optional");
@@ -1134,7 +1127,7 @@ public:
11341127

11351128
template <invocable _Func>
11361129
_LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
1137-
requires is_move_constructible_v<value_type>
1130+
requires is_move_constructible_v<_Tp>
11381131
{
11391132
static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
11401133
"Result of f() should be the same type as this optional");

libcxx/test/std/utilities/optional/optional.object/types.pass.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ int main(int, char**)
3636
test<optional<const int>, const int>();
3737
test<optional<double>, double>();
3838
test<optional<const double>, const double>();
39-
40-
return 0;
39+
#if TEST_STD_VER >= 26
40+
test<optional<int&>, int>();
41+
test<optional<const int&>, const int>();
42+
test<optional<double&>, double>();
43+
test<optional<const double&>, const double>();
44+
#endif
45+
return 0;
4146
}

0 commit comments

Comments
 (0)