本文介绍了是否保证在返回对象时移动对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当一个对象通过值传递给一个函数时,move constructor总是被调用,如果有一个,假设没有复制elision。例如,假设我们有一个类 Foo ,它有一个移动构造函数,我们有一个返回 Foo 对象的函数。

  Foo g(){
Foo f;

//使用f $ b执行某些操作
$ b return f;
}



如果我们假设没有RVO, ?



更新:我想我没有清楚表明我的意图。我只是想知道我可以在最坏的情况下有对象移动没有复制。发生RVO或NRVO,我很高兴。我还应该说,移动构造函数和移动赋值不会被删除,并且正确实现。

解决方案

参见[class.copy] p32


I know that when passing an object by value to a function, the move constructor is always called if there is one, assuming no copy elision. What about returning an object by value?

For example, say we have a class Foo which has a move constructor, and we have a function that returns a Foo object.

Foo g() {
    Foo f;

    // do something with f

    return f;
}

If we assume there is no RVO, is the move constructor guaranteed to be called?

Update: I guess I didn't show my intention clearly. I just want to know I can in the worst case have the object moved not copied. Either RVO or NRVO happens, I am happy. And I should also say that move constructor and move assignment are not deleted and are properly implemented.

解决方案

Yes. See [class.copy] p32

这篇关于是否保证在返回对象时移动对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:06