Difference between “return-by-rvalue-ref” & “return-by-value” when you return using std::move?

我与影子孤独终老i 提交于 2019-11-29 04:08:51

Considerations aside on whether the program you wrote actually makes sense (moving from a data member is awkward - but OK, perhaps there are some use cases), in this case the two versions of that function end up doing the same thing.

As general practice, however, you should prefer returning by value, because in many cases it allows the compiler to perform copy elision and elide the calls to the move constructor of the returned type, as permitted by paragraph 12.8/31 of the C++11 Standard.

Copy elision allows the compiler to create the return value of the function directly in the object which should be initialized from the function's return value.

Therefore, as a general guideline, prefer returning by value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!