与Fowarding参考混淆

发布于 2025-02-06 04:22:35 字数 811 浏览 0 评论 0原文

评论中说:

将LVALUE转发为LVALUE或RVALUE

但是返回值只是rvalue的参考,我想知道它是如何是lavlue或rvalue?这是否意味着返回的值是通用参考?如果是这样,什么决定如何将其推导为lvalue参考或rvalue参考?

另外,第二个过载返回完全相同的内容,为什么它在注释中仅作为无用参考的RVALUE参考说出?

template <class _Ty>
    _NODISCARD constexpr _Ty&& forward(
        remove_reference_t<_Ty>& _Arg) noexcept { // forward an lvalue as either an lvalue or an rvalue
        return static_cast<_Ty&&>(_Arg);
    }
    
    template <class _Ty>
    _NODISCARD constexpr _Ty&& forward(remove_reference_t<_Ty>&& _Arg) noexcept { // forward an rvalue as an rvalue
        static_assert(!is_lvalue_reference_v<_Ty>, "bad forward call");
        return static_cast<_Ty&&>(_Arg);
    }

In the c++ std type_traits file below the first overloaded function, the comment says:

forward an lvalue as either an lvalue or an rvalue

However the return value is just an rvalue reference, I wonder how it could be either an lavlue or an rvalue? Does it mean the returned value is a universal reference? If so what decides how to deduce it to an lvalue reference or an rvalue reference?

Also the second overload returns exactly the same thing, why does it say in the comment only forwarding as a rvalue reference without lvalue reference?

template <class _Ty>
    _NODISCARD constexpr _Ty&& forward(
        remove_reference_t<_Ty>& _Arg) noexcept { // forward an lvalue as either an lvalue or an rvalue
        return static_cast<_Ty&&>(_Arg);
    }
    
    template <class _Ty>
    _NODISCARD constexpr _Ty&& forward(remove_reference_t<_Ty>&& _Arg) noexcept { // forward an rvalue as an rvalue
        static_assert(!is_lvalue_reference_v<_Ty>, "bad forward call");
        return static_cast<_Ty&&>(_Arg);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情丝乱 2025-02-13 04:22:35

如果_TY是rvalue参考或非参考,则_TY&amp;&amp; is(通过参考删除规则)rvalue参考。因此,函数调用表达式将是xvalue(一种rvalue)。

但是,如果_ty是对类型t的LVALUE引用,则IE _TY = t&amp;,则参考删除规则也暗示_TY&amp ;&amp; = T&amp; &amp;&amp; = t&amp;。因此,函数调用将是LVALUE表达式。

这是std :: forward的实现。 _TY的模板参数不打算(也不能)推导。相反,必须明确给出它。

通常,std :: forward仅应以std :: forward&lt; u&gt;(u);使用u是转发参考参考表单的参数u&amp;&amp; u在功能模板中使用u模板参数。

在这些条件下,如果通过u传递了rvalue,u将被推荐为非参考,并且如果通过lvalue传递了u将推荐给LVALUE参考。

然后std :: forward&lt; u&gt;(u)将传递非参考或lvalue参考类型作为_TY> _TY to std :: forthrom to thork to to thork toefform std :: fromphst ::相应地和以上规则,std :: forward&lt; u&gt;(u)将具有相同的值类别(rvalue或lvalue)与转发参考u的参数相同有。 (但是,它将prvalues和xvalues映射到xvalues。)


在第二个超载中,该注释不会提到转发为lvalue,因为如果用户尝试使用这种方式,则static_assert将触发。在参数是rvalue时,不应允许使用lvalue参考模板参数调用std :: forward。这与我上面讨论的预期用法不符。

If _Ty is either a rvalue reference or a non-reference, then _Ty&& is (by reference collapsing rules) a rvalue reference. Hence the function call expression will be a xvalue (a kind of rvalue).

If _Ty is however an lvalue reference to type T, i.e. _Ty = T&, then the reference collapsing rules imply that also _Ty&& = T & && = T&. So then the function call will be an lvalue expression.

This is an implementation of std::forward. The template argument for _Ty is not intended to (and can't be) deduced. Instead it must be given explicitly.

Normally std::forward should only be used in the form std::forward<U>(u); where u is a forwarding reference parameter of the form U&& u in a function template with U a template parameter.

Under these conditions, if a rvalue was passed for u, U will be deduced to a non-reference and if an lvalue was passed U will be deduced to an lvalue reference.

Then std::forward<U>(u) will pass either a non-reference or an lvalue reference type as template argument for _Ty to std::forward accordingly and by the rule above, std::forward<U>(u) will have the same value category (rvalue or lvalue) as the argument to the forwarding reference u had. (However, it maps both prvalues and xvalues to xvalues.)


In the second overload the comment doesn't mention forwarding as lvalue, because the static_assert will trigger if the user tried to use it that way. It should not be allowed to call std::forward with a lvalue reference template argument while the argument is a rvalue. That would not match the intended usage I discussed above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文