Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return && object from function? [duplicate]

I have TTempTable class with move symantics. I wrote

TTempTable&& MyFunction() {
   TTempTable tmp = f(...);
   ...
   return std::move(tmp);
}

and got no compiler errors.

Was this correct?

like image 562
Dims Avatar asked May 27 '19 12:05

Dims


People also ask

How do I return an item?

Tell the clerk you want to return the item. Go to the returns department if there is one, or to a cashier. Smile and explain that you want to return an item and why. Remember to be friendly.

What to say to return an item?

Tell the clerk you want to return the item. Smile and say, "Hi, I want to return this item which I bought last week." Show the clerk the item and your receipt. Don't delay returning the item. Some stores allow returns but only for a certain amount of time.


1 Answers

No, it is not correct.

You're returning a reference to a local variable. That reference is dangling.

Like any dangling thing, the compiler won't [always] diagnose it for you.

Return by value, and remove the std::move (it's redundant and inhibits elision).

like image 100
Lightness Races in Orbit Avatar answered Oct 06 '22 20:10

Lightness Races in Orbit