7

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?

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021
Dims
  • 42,427
  • 94
  • 291
  • 543
  • 1
    Have a look at [this rule in the C++ Core Guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f45-dont-return-a-t) – GPhilo May 27 '19 at 12:56

1 Answers1

20

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).

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021