This question already has answers here:
C++11 rvalues and move semantics confusion (return statement)

(6个答案)


2年前关闭。




我有移动技巧的TTempTable类。我写
TTempTable&& MyFunction() {
   TTempTable tmp = f(...);
   ...
   return std::move(tmp);
}

并且没有编译器错误。

这是正确的吗?

最佳答案

不,这是不正确的。

你是returning a reference to a local variable。那个引用悬而未决。

像任何悬而未决的事情一样,编译器不会(总是)为您诊断它。

按值返回,并删除std::move(it's redundant and inhibits elision)。

关于c++ - 如何从函数返回&&对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56326523/

10-12 21:31