[libcxx-commits] [libcxx] [libc++] Introduce __forward_as (PR #118168)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Nov 30 03:33:58 PST 2024
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/118168
This allows forwarding an object as a specific type. This is usually useful when using `deducing this` to avoid calling any functions in a deriving class.
>From cbd4155ab11aedafa14fccf1f0b05b2be8e0a2bf Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sat, 30 Nov 2024 12:31:53 +0100
Subject: [PATCH] [libc++] Introduce __forward_as
---
libcxx/include/__utility/forward_like.h | 8 ++++++++
libcxx/include/variant | 6 ++----
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/libcxx/include/__utility/forward_like.h b/libcxx/include/__utility/forward_like.h
index 0206ce23a56681..4a3bc240c3d9db 100644
--- a/libcxx/include/__utility/forward_like.h
+++ b/libcxx/include/__utility/forward_like.h
@@ -12,6 +12,7 @@
#include <__config>
#include <__type_traits/conditional.h>
+#include <__type_traits/is_base_of.h>
#include <__type_traits/is_const.h>
#include <__type_traits/is_reference.h>
#include <__type_traits/remove_reference.h>
@@ -39,6 +40,13 @@ forward_like(_LIBCPP_LIFETIMEBOUND _Up&& __ux) noexcept -> _ForwardLike<_Tp, _Up
return static_cast<_ForwardLike<_Tp, _Up>>(__ux);
}
+template <class _Tp, class _As, class _Up>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _ForwardLike<_Tp, _As>
+__forward_as(_LIBCPP_LIFETIMEBOUND _Up&& __val) noexcept {
+ static_assert(is_base_of_v<_As, remove_reference_t<_Up>>);
+ return static_cast<_ForwardLike<_Tp, _As>>(__val);
+}
+
#endif // _LIBCPP_STD_VER >= 23
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/variant b/libcxx/include/variant
index f604527cd22569..7e44ec54141c23 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -1309,14 +1309,12 @@ public:
template <__variant_visit_barrier_tag = __variant_visit_barrier_tag{}, class _Self, class _Visitor>
_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(this _Self&& __self, _Visitor&& __visitor) {
- using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>;
- return std::visit(std::forward<_Visitor>(__visitor), (_VariantT)__self);
+ return std::visit(std::forward<_Visitor>(__visitor), std::__forward_as<_Self, variant>(__self));
}
template <class _Rp, class _Self, class _Visitor>
_LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(this _Self&& __self, _Visitor&& __visitor) {
- using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>;
- return std::visit<_Rp>(std::forward<_Visitor>(__visitor), (_VariantT)__self);
+ return std::visit<_Rp>(std::forward<_Visitor>(__visitor), std::__forward_as<_Self, variant>(__self));
}
# endif
More information about the libcxx-commits
mailing list