要返回std :: move(x)或不? [英] To return std::move (x) or not?
本文介绍了要返回std :: move(x)或不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是
std :: vector< double> foo()
{
std :: vector< double> t;
...
return t;
}
和
std :: vector< double> foo()
{
std :: vector< double> t;
...
return std :: move(t);
}
当量?
更准确地说, return x
总是等于 return std :: move(x)
?
解决方案
它们不是等效的,你应该总是使用 return t;
p>
更长的版本是,当且仅当return语句有资格进行返回值优化时,returnee绑定到rvalue引用(或者俗称为 move 是隐式的)。
通过拼写出来 return std :: move(t);
,但是,你实际上禁止返回值优化!
Are
std::vector<double> foo ()
{
std::vector<double> t;
...
return t;
}
and
std::vector<double> foo ()
{
std::vector<double> t;
...
return std::move (t);
}
equivalent ?
More precisely, is return x
always equivalent to return std::move (x)
?
解决方案
They're not equivalent, and you should always use return t;
.
The longer version is that if and only if a return statement is eligible for return value optimization, then the returnee binds to rvalue reference (or colloquially, "the move
is implicit").
By spelling out return std::move(t);
, however, you actually inhibit return value optimization!
这篇关于要返回std :: move(x)或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文