Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #87888 > unrolled thread

R-value references and std::forward

Started byFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
First post2022-12-14 00:44 -0800
Last post2022-12-17 10:55 -0800
Articles 10 — 6 participants

Back to article view | Back to comp.lang.c++


Contents

  R-value references and std::forward Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-14 00:44 -0800
    Re: R-value references and std::forward Bo Persson <bo@bo-persson.se> - 2022-12-14 10:26 +0100
      Re: R-value references and std::forward Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-14 01:48 -0800
        Re: R-value references and std::forward Bo Persson <bo@bo-persson.se> - 2022-12-14 12:42 +0100
          Re: R-value references and std::forward Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-14 06:46 -0800
            Re: R-value references and std::forward Öö Tiib <ootiib@hot.ee> - 2022-12-15 05:48 -0800
        Re: R-value references and std::forward Juha Nieminen <nospam@thanks.invalid> - 2022-12-14 12:41 +0000
          Re: R-value references and std::forward Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-15 19:03 +0100
    Re: R-value references and std::forward Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-15 16:39 +0100
    Re: R-value references and std::forward Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-17 10:55 -0800

#87888 — R-value references and std::forward

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2022-12-14 00:44 -0800
SubjectR-value references and std::forward
Message-ID<6d19a662-76a1-4925-bd07-b259836f062bn@googlegroups.com>
The following code up on GodBolt:

    https://godbolt.org/z/bfs3qn5Gx

prints out:

    Dog takes l-value ref
    Monkey takes r-value ref
    Hello

I expected it to say "Monkey takes l-value ref".

Is the behaviour undefined because of my use of 'forward' on Line #22? Should 'forward' only be used with a so-called 'template parameter forwarding reference'?

[toc] | [next] | [standalone]


#87889

FromBo Persson <bo@bo-persson.se>
Date2022-12-14 10:26 +0100
Message-ID<jvtj6mFq5jfU1@mid.individual.net>
In reply to#87888
On 2022-12-14 at 09:44, Frederick Virchanza Gotham wrote:
> The following code up on GodBolt:
> 
>      https://godbolt.org/z/bfs3qn5Gx
> 
> prints out:
> 
>      Dog takes l-value ref
>      Monkey takes r-value ref
>      Hello
> 
> I expected it to say "Monkey takes l-value ref".
> 
> Is the behaviour undefined because of my use of 'forward' on Line #22? Should 'forward' only be used with a so-called 'template parameter forwarding reference'?
> 

You are using it wrong, anyway. When the value is an int&, you should 
"forward" is using forward<int&>(arg).

That's what happens with a forwarding reference.


[toc] | [prev] | [next] | [standalone]


#87895

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2022-12-14 01:48 -0800
Message-ID<a4ffe6c9-868a-4a12-9ab2-49094d7466c8n@googlegroups.com>
In reply to#87889
On Wednesday, December 14, 2022 at 9:27:06 AM UTC, Bo Persson wrote:

> You are using it wrong, anyway. When the value is an int&, you should 
> "forward" is using forward<int&>(arg). 
> 
> That's what happens with a forwarding reference.


If I knew for certain that it was an "int&" then why would I bother using 'forward' on it?

The question I'm asking is why 'forward' in this case is turning an L-value into an R-value?
Is it because it's UB to use 'forward' on something that isn't a "template parameter forwarding reference"?

[toc] | [prev] | [next] | [standalone]


#87898

FromBo Persson <bo@bo-persson.se>
Date2022-12-14 12:42 +0100
Message-ID<jvtr5nFrbl6U1@mid.individual.net>
In reply to#87895
On 2022-12-14 at 10:48, Frederick Virchanza Gotham wrote:
> On Wednesday, December 14, 2022 at 9:27:06 AM UTC, Bo Persson wrote:
> 
>> You are using it wrong, anyway. When the value is an int&, you should
>> "forward" is using forward<int&>(arg).
>>
>> That's what happens with a forwarding reference.
> 
> 
> If I knew for certain that it was an "int&" then why would I bother using 'forward' on it?

Beats me, it was your example.

> 
> The question I'm asking is why 'forward' in this case is turning an L-value into an R-value?

Because that's how template argument deduction works. When you 
explicitly supply <int> to a function returning T&&, you get an int&&. 
No surprise here.

> Is it because it's UB to use 'forward' on something that isn't a "template parameter forwarding reference"?

No, but not very useful. The whole idea of std::forward is that it can 
adapt to the deduced template parameter. If there is no template, it is 
outside if the intended use pattern.

[toc] | [prev] | [next] | [standalone]


#87910

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2022-12-14 06:46 -0800
Message-ID<9bb7d8bb-e342-4e8f-8981-c7b74806a7d9n@googlegroups.com>
In reply to#87898
On Wednesday, December 14, 2022 at 11:43:06 AM UTC, Bo Persson wrote:

> Because that's how template argument deduction works. When you 
> explicitly supply <int> to a function returning T&&, you get an int&&. 
> No surprise here.


It all makes sense after learning about the "reference collapsing" rules.

Although to be honest it does surprise me that those reference collapsing rules made it into the Standard.

[toc] | [prev] | [next] | [standalone]


#87955

FromÖö Tiib <ootiib@hot.ee>
Date2022-12-15 05:48 -0800
Message-ID<bc7b2445-b549-4599-ae93-afc758fbb997n@googlegroups.com>
In reply to#87910
On Wednesday, 14 December 2022 at 16:46:59 UTC+2, Frederick Virchanza Gotham wrote:
> On Wednesday, December 14, 2022 at 11:43:06 AM UTC, Bo Persson wrote: 
> 
> > Because that's how template argument deduction works. When you 
> > explicitly supply <int> to a function returning T&&, you get an int&&. 
> > No surprise here.
> It all makes sense after learning about the "reference collapsing" rules. 
> 
> Although to be honest it does surprise me that those reference collapsing rules made it into the Standard.

That is empty critique as it lacks constructive explanation what rules
and/or wording you would expect to be better.

[toc] | [prev] | [next] | [standalone]


#87903

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-12-14 12:41 +0000
Message-ID<tncgah$1tol$1@gioia.aioe.org>
In reply to#87895
Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> wrote:
> If I knew for certain that it was an "int&" then why would I bother
> using 'forward' on it?

std::forward is useful mainly with templated types. It's not all that
useful with concrete types.

The main idea with std::forward is that it preserves lvalues as lvalues
and rvalues as rvalues when passing parameters to other functions (if
you don't use std::forward and tried to pass the function parameter
directly then an rvalue would decay to an lvalue, because a named
variable always decays to an lvalue, unless explicitly cast to an
rvalue). In order to do this you need to provide it with the original
type of the value you are passing, as a template parameter. This
would usually be the original template type that that parameter has.

[toc] | [prev] | [next] | [standalone]


#87964

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-15 19:03 +0100
Message-ID<tnfnh9$34t1j$1@dont-email.me>
In reply to#87903
Am 14.12.2022 um 13:41 schrieb Juha Nieminen:

> The main idea with std::forward is that it preserves lvalues as lvalues
> and rvalues as rvalues when passing parameters to other functions ...

And it preserves const-ness, even with r-value references.

[toc] | [prev] | [next] | [standalone]


#87958

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-15 16:39 +0100
Message-ID<tnff29$345p0$1@dont-email.me>
In reply to#87888
Am 14.12.2022 um 09:44 schrieb Frederick Virchanza Gotham:
> The following code up on GodBolt:
> 
>      https://godbolt.org/z/bfs3qn5Gx
> 
> prints out:
> 
>      Dog takes l-value ref
>      Monkey takes r-value ref
>      Hello
> 
> I expected it to say "Monkey takes l-value ref".
> 
> Is the behaviour undefined because of my use of 'forward' on Line #22? Should 'forward' only be used with a so-called 'template parameter forwarding reference'?
> 

std::forward is for templated forwarding-references, not references you
know that they're r-value-references. With that you can use move() to
have a somewhat clearer code. And moving ints looks strange to me.

[toc] | [prev] | [next] | [standalone]


#87994

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2022-12-17 10:55 -0800
Message-ID<tnl3bf$3na58$1@dont-email.me>
In reply to#87888
On 12/14/2022 12:44 AM, Frederick Virchanza Gotham wrote:
> Is the behaviour undefined because of my use of 'forward' on Line #22? 

No. Why would it be? The behavior of `std::forward<>` is well-specified. 
Regardless of how and where you use it, it will simply behave in 
accordance with its specification.

> Should 'forward' only be used with a so-called 'template parameter forwarding reference'?

Yes, it probably should. That's what it's for.

-- 
Best regards,
Andrey

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web