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

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 28 02:33:10 PDT 2026


https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/212443

>From 3970abdd4df06fcd9c8047055be083eb5fe26a2d Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 28 Jul 2026 12:19:13 +0300
Subject: [PATCH] [libc++][optional][NFC] Use variable templates instead of
 class templates

Similar to: https://github.com/llvm/llvm-project/pull/199481
---
 libcxx/include/optional | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/libcxx/include/optional b/libcxx/include/optional
index 2499479348892..2acd496531380 100644
--- a/libcxx/include/optional
+++ b/libcxx/include/optional
@@ -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 const bool __is_std_optional_v = false;
+
 template <class _Tp>
-struct __is_std_optional<optional<_Tp>> : true_type {};
+inline const 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>();



More information about the libcxx-commits mailing list