C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Automatic perfect forwarding is possible and not too complicated

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Sun, 13 Apr 2025 18:44:02 +0100
On Sun, 13 Apr 2025, 17:22 Jonathan Wakely, <cxx_at_[hidden]> wrote:

>
>
> On Sun, 13 Apr 2025, 16:46 Robin Savonen Söderholm via Std-Proposals, <
> std-proposals_at_[hidden]> wrote:
>
>> But '&&' does not mean 'forwarding reference'. It means 'rvalue
>> reference'. It's just that an rvalue reference of an lvalue reference is an
>> lvalue reference.
>>
>
> That's not really true. It does have special magic rules in the context of
> a deduced template parameter. It's not just reference collapsing.
>

To expand on this, you cannot generally bind an rvalue reference to an
lvalue:

X x;
X&& r = x; // ill-formed

But you can with a T&& function parameter when T is a deduced type:

template<typename T> void f(T&&);
f(x);

Or simply:

auto&& r = x;

If the && in these deduction cases was just an rvalue reference then they
wouldn't work. It requires a special language rule that deduces either an
lvalue or rvalue type. So there really is a special thing that we call a
forwarding reference, which is a reference to a deduced type.

Received on 2025-04-13 17:44:23