[libcxx-commits] [libcxx] [libc++][NFC] Replace tag dispatch with `if constexpr` in `<any>` and `<variant>` (PR #173538)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 25 02:01:40 PST 2025


https://github.com/frederick-vs-ja created https://github.com/llvm/llvm-project/pull/173538

Also simplify `any_cast<FunctionType>(ptr)` to directly return `nullptr`.

>From b198800c9d1fbf508d360997925c78c46b3664da Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Thu, 25 Dec 2025 17:58:10 +0800
Subject: [PATCH] [libc++][NFC] Replace tag dispatch with `if constexpr` in
 `<any>` and `<variant>`

Also simplify `any_cast<FunctionType>(ptr)` to directly return `nullptr`.
---
 libcxx/include/any     | 32 ++++++++++++--------------------
 libcxx/include/variant | 16 +++++-----------
 2 files changed, 17 insertions(+), 31 deletions(-)

diff --git a/libcxx/include/any b/libcxx/include/any
index 5c779e397c9ea..91bd23ec96890 100644
--- a/libcxx/include/any
+++ b/libcxx/include/any
@@ -544,33 +544,25 @@ template <class _ValueType>
   return std::any_cast<_ValueType>(const_cast<any*>(__any));
 }
 
-template <class _RetType>
-inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void* __p, /*IsFunction*/ false_type) noexcept {
-  return static_cast<_RetType>(__p);
-}
-
-template <class _RetType>
-inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void*, /*IsFunction*/ true_type) noexcept {
-  return nullptr;
-}
-
 template <class _ValueType>
-[[nodiscard]] _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) _NOEXCEPT {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast([[maybe_unused]] any* __any) _NOEXCEPT {
   using __any_imp::_Action;
   static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
   static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
-  typedef add_pointer_t<_ValueType> _ReturnType;
-  if (__any && __any->__h_) {
-    void* __p = __any->__call(
-        _Action::_Get,
-        nullptr,
+  if constexpr (!is_function_v<_ValueType>) {
+    using _ReturnType = add_pointer_t<_ValueType>;
+    if (__any && __any->__h_) {
+      void* __p = __any->__call(
+          _Action::_Get,
+          nullptr,
 #    if _LIBCPP_HAS_RTTI
-        &typeid(_ValueType),
+          &typeid(_ValueType),
 #    else
-        nullptr,
+          nullptr,
 #    endif
-        __any_imp::__get_fallback_typeid<_ValueType>());
-    return std::__pointer_or_func_cast<_ReturnType>(__p, is_function<_ValueType>{});
+          __any_imp::__get_fallback_typeid<_ValueType>());
+      return static_cast<_ReturnType>(__p);
+    }
   }
   return nullptr;
 }
diff --git a/libcxx/include/variant b/libcxx/include/variant
index b93009b3353cb..a43910798e7f1 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -918,17 +918,11 @@ protected:
     if (this->index() == _Ip) {
       __a.__value = std::forward<_Arg>(__arg);
     } else {
-      struct {
-        _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()(true_type) const {
-          __this->__emplace<_Ip>(std::forward<_Arg>(__arg));
-        }
-        _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()(false_type) const {
-          __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
-        }
-        __assignment* __this;
-        _Arg&& __arg;
-      } __impl{this, std::forward<_Arg>(__arg)};
-      __impl(bool_constant < is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v < _Tp >> {});
+      if constexpr (is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v<_Tp>) {
+        this->__emplace<_Ip>(std::forward<_Arg>(__arg));
+      } else {
+        this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
+      }
     }
   }
 



More information about the libcxx-commits mailing list