Skip to content

[libc++] Introduce __forward_as #118168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions libcxx/include/__utility/forward_like.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -39,6 +40,22 @@ forward_like(_LIBCPP_LIFETIMEBOUND _Up&& __ux) noexcept -> _ForwardLike<_Tp, _Up
return static_cast<_ForwardLike<_Tp, _Up>>(__ux);
}

// This function is used for `deducing this` cases where you want to make sure the operation is performed on the class
// itself and not on a derived class. For example
// struct S {
// template <class Self>
// void func(Self&& self) {
// // This will always call `do_something` of S instead of any class derived from S.
// std::__forward_as<Self, S>(self).do_something();
// }
// };
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
Expand Down
6 changes: 2 additions & 4 deletions libcxx/include/variant
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading