Date: Fri, 5 Sep 2025 21:15:14 -0400
> There is no way to tell, from inside the function block, if the parameter
> passed to this function is a prvalue or an xrvalue
I assume that you meant to say xvalue instead of xrvalue. The reason they
can't be distinguished is that it's not possible to forward a prvalue. Consider:
struct S{
S();
S(S&&)=delete;
};
void foo(S);
void bar(S s){
//...
}
The class S cannot be copied nor moved. There is no mechanism currently that
would allow for forwarding bar's parameter to foo, since to achieve something
like that would mean the caller of bar would somehow need to know where bar will
forward it to construct the object correctly.
When taking a prvalue of type T there are only two types of parameters that
would make sense: T and T&&. As described above, when the parameter has type T
it can still only be forwarded as an xvalue. So, when just forwarding the T
there generally won't be any differences. The parameter having type T&& has a
few advantages though. If a reference to the object is returned, the caller can
use that object inside of the full expression which the call is in. When the
parameter has type T it might be destroyed at the end of the call. For generic
code that forwards arbitrary types, using T&& simplifies code since it can rely
on reference collapsing and that the parameter has a reference type.
On Fri, Sep 5, 2025 at 8:18 PM organicoman via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hello,
> Given the following function signature:
>
> RetType foo(T&& arg);
>
> There is no way to tell, from inside the function block, if the parameter passed to this function is a prvalue or an xrvalue, since both can bind to T&&.
> Despite there is a fundamental difference between prvalue and xrvalue, yet we are not taking advantage of that because we cannot differentiate between them.
>
> Is there any proposal talking about this problem?
>
> Regards
> Og
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> passed to this function is a prvalue or an xrvalue
I assume that you meant to say xvalue instead of xrvalue. The reason they
can't be distinguished is that it's not possible to forward a prvalue. Consider:
struct S{
S();
S(S&&)=delete;
};
void foo(S);
void bar(S s){
//...
}
The class S cannot be copied nor moved. There is no mechanism currently that
would allow for forwarding bar's parameter to foo, since to achieve something
like that would mean the caller of bar would somehow need to know where bar will
forward it to construct the object correctly.
When taking a prvalue of type T there are only two types of parameters that
would make sense: T and T&&. As described above, when the parameter has type T
it can still only be forwarded as an xvalue. So, when just forwarding the T
there generally won't be any differences. The parameter having type T&& has a
few advantages though. If a reference to the object is returned, the caller can
use that object inside of the full expression which the call is in. When the
parameter has type T it might be destroyed at the end of the call. For generic
code that forwards arbitrary types, using T&& simplifies code since it can rely
on reference collapsing and that the parameter has a reference type.
On Fri, Sep 5, 2025 at 8:18 PM organicoman via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hello,
> Given the following function signature:
>
> RetType foo(T&& arg);
>
> There is no way to tell, from inside the function block, if the parameter passed to this function is a prvalue or an xrvalue, since both can bind to T&&.
> Despite there is a fundamental difference between prvalue and xrvalue, yet we are not taking advantage of that because we cannot differentiate between them.
>
> Is there any proposal talking about this problem?
>
> Regards
> Og
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-09-06 01:15:48