[libc++][optional][NFC] Use variable templates instead of class templates (#212443)

Modernizing similar to: https://github.com/llvm/llvm-project/pull/199481
This commit is contained in:
Hristo Hristov 2026-07-28 18:12:56 +03:00 committed by GitHub
parent 62f429ec6e
commit 0fc8ce3bb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -757,9 +757,10 @@ concept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(co
# endif // _LIBCPP_STD_VER >= 20
template <class _Tp>
struct __is_std_optional : false_type {};
inline constexpr bool __is_std_optional_v = false;
template <class _Tp>
struct __is_std_optional<optional<_Tp>> : true_type {};
inline constexpr bool __is_std_optional_v<optional<_Tp>> = true;
# if _LIBCPP_STD_VER < 26
template <class _Tp>
@ -903,7 +904,7 @@ private:
template <class _Up>
using _CheckOptionalArgsCtor _LIBCPP_NODEBUG =
_If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value &&
(!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value),
(!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional_v<__remove_cvref_t<_Up>>),
_CheckOptionalArgsConstructor,
__check_tuple_constructor_fail >;
template <class _QualUp>
@ -1093,8 +1094,8 @@ public:
template <class _Func>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
using _Up = invoke_result_t<_Func, _Tp&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
"Result of f(value()) must be a specialization of std::optional");
static_assert(
__is_std_optional_v<remove_cvref_t<_Up>>, "Result of f(value()) must be a specialization of std::optional");
if (*this)
return std::invoke(std::forward<_Func>(__f), value());
return remove_cvref_t<_Up>();
@ -1103,8 +1104,8 @@ public:
template <class _Func>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
using _Up = invoke_result_t<_Func, const _Tp&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
"Result of f(value()) must be a specialization of std::optional");
static_assert(
__is_std_optional_v<remove_cvref_t<_Up>>, "Result of f(value()) must be a specialization of std::optional");
if (*this)
return std::invoke(std::forward<_Func>(__f), value());
return remove_cvref_t<_Up>();
@ -1113,7 +1114,7 @@ public:
template <class _Func>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
using _Up = invoke_result_t<_Func, _Tp&&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
static_assert(__is_std_optional_v<remove_cvref_t<_Up>>,
"Result of f(std::move(value())) must be a specialization of std::optional");
if (*this)
return std::invoke(std::forward<_Func>(__f), std::move(value()));
@ -1123,7 +1124,7 @@ public:
template <class _Func>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
using _Up = invoke_result_t<_Func, const _Tp&&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
static_assert(__is_std_optional_v<remove_cvref_t<_Up>>,
"Result of f(std::move(value())) must be a specialization of std::optional");
if (*this)
return std::invoke(std::forward<_Func>(__f), std::move(value()));
@ -1338,8 +1339,8 @@ public:
template <class _Func>
[[nodiscard]] constexpr auto and_then(_Func&& __f) const {
using _Up = invoke_result_t<_Func, _Tp&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
"Result of f(value()) must be a specialization of std::optional");
static_assert(
__is_std_optional_v<remove_cvref_t<_Up>>, "Result of f(value()) must be a specialization of std::optional");
if (*this)
return std::invoke(std::forward<_Func>(__f), value());
return remove_cvref_t<_Up>();