[libcxx-commits] [libcxx] b81c1bb - [libc++][NFC] Replace tag dispatch with `if constexpr` in `<any>` and `<variant>` (#173538)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jan 5 22:09:49 PST 2026
Author: A. Jiang
Date: 2026-01-06T14:09:45+08:00
New Revision: b81c1bb1a1494e313ffc21f42020f04ae9fbf995
URL: https://github.com/llvm/llvm-project/commit/b81c1bb1a1494e313ffc21f42020f04ae9fbf995
DIFF: https://github.com/llvm/llvm-project/commit/b81c1bb1a1494e313ffc21f42020f04ae9fbf995.diff
LOG: [libc++][NFC] Replace tag dispatch with `if constexpr` in `<any>` and `<variant>` (#173538)
Also simplify `any_cast<FunctionType>(ptr)` to directly return
`nullptr`.
Added:
Modified:
libcxx/include/any
libcxx/include/variant
Removed:
################################################################################
diff --git a/libcxx/include/any b/libcxx/include/any
index 5c779e397c9ea..d853ee0cb6885 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 {
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..56c5efe92b642 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -917,18 +917,10 @@ protected:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
if (this->index() == _Ip) {
__a.__value = std::forward<_Arg>(__arg);
+ } else if constexpr (is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v<_Tp>) {
+ this->__emplace<_Ip>(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 >> {});
+ this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
}
}
More information about the libcxx-commits
mailing list