Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function returning &&

52 views
Skip to first unread message

Joseph Hesse

unread,
Jan 5, 2015, 10:57:16 AM1/5/15
to
Hi,

Can someone provide me with an example of a function returning an &&.
Preferably something simple such as:
int && f(int);

I tried
int && f(int x) {
return x;
}
but it didn't work.

Thank you,
Joe

Luca Risolia

unread,
Jan 5, 2015, 11:11:36 AM1/5/15
to
#include <utility>

int && f(int x) {
return std::move(x);
}

Message has been deleted

Bo Persson

unread,
Jan 5, 2015, 11:22:06 AM1/5/15
to
This seems to work, but returns a dangling reference to the parameter.

About the only use case I have seen is the std::move function itself
(which takes its parameter by reference, and not by value).


Bo Persson

Joseph Hesse

unread,
Jan 5, 2015, 11:22:21 AM1/5/15
to
Why doesn't this work:

int && f()
{
int && x = 10;
return x;
}

In the above, x is clearly an int && and that is what the function
promises to return.

Öö Tiib

unread,
Jan 5, 2015, 11:27:33 AM1/5/15
to
On Monday, 5 January 2015 17:57:16 UTC+2, Joseph Hesse wrote:
> Hi,
>
> Can someone provide me with an example of a function returning an &&.

Who returns it must be make sure that the object does not go out of scope
before it is used by caller.

struct Thing
{
Resource r;

// return reference to member; caller can use it for copying
Resource const& resource() const
{
return r;
}

// return rvalue reference to caller; caller can use it for moving
Resource&& stealResource() &&
{
return std::move(r);
}
};

> I tried
> int && f(int x) {
> return x;
> }
> but it didn't work.

This is trying to return dangling reference to function's by-value parameter.
Changing to 'return std::move(x);' will succeed in returning that dangling
reference. You don't want to return dangling references because it is undefined
behaviour.

Victor Bazarov

unread,
Jan 5, 2015, 11:27:38 AM1/5/15
to
"Doesn't work" likely because you're returning a reference to a
non-existent object (a temporary whose lifetime ends when the function
body is exited).

V
--
I do not respond to top-posted replies, please don't ask

Luca Risolia

unread,
Jan 5, 2015, 1:15:35 PM1/5/15
to
On 05/01/2015 17:21, Bo Persson wrote:
> On 2015-01-05 17:11, Luca Risolia wrote:
>> On 05/01/2015 16:57, Joseph Hesse wrote:
>>> Can someone provide me with an example of a function returning an &&.
>>> Preferably something simple such as:
>>> int && f(int);
>>>
>>> I tried
>>> int && f(int x) {
>>> return x;
>>> }
>>> but it didn't work.
>>
>> #include <utility>
>>
>> int && f(int x) {
>> return std::move(x);
>> }
>>
>
> This seems to work, but returns a dangling reference to the parameter.

Sure, I should have added that. I read "did not work" as "did not
compile" - as if the OP was trying to experiment some stuff.

Luca Risolia

unread,
Jan 5, 2015, 1:41:38 PM1/5/15
to
On 05/01/2015 17:22, Joseph Hesse wrote:
> int && f()
> {
> int && x = 10;
> return x;
> }
>
> In the above, x is clearly an int && and that is what the function
> promises to return.

No, in short, the expression "x" refers to an lvalue, after x has been
initialized. There are valid reasons why this happens. If you are
interested, there should be some more detailed explanations at the
Stroustrup's web page:

http://www.stroustrup.com/C++11FAQ.html

0 new messages