与此有些相关,我有一个关于返回const
以及这如何影响 RVO 和移动的问题。我通常喜欢让我的函数返回const
如下对象:
// Return const to help ensure the potential temporary can't be used incorrectly
const std::vector<int> foo()
{
std::vector<int> bar(10, 42); // Note: this is not const
return bar;
}
现在我有一个关于这如何影响 RVO 和移动的问题。如果我执行以下操作:
std::vector<int> myVector = foo();
的返回类型是否会抑制const
RVOfoo
或移动?即使局部变量foo
本身不是const
?如果我想利用 RVO 并移动语义,我应该摆脱const
?