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
  • 378,754
  • 76
  • 643
  • 1,055
Dims
  • 47,675
  • 117
  • 331
  • 600
  • 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
  • 378,754
  • 76
  • 643
  • 1,055