Can a C++ compiler perform RVO for a const return value?

♀尐吖头ヾ 提交于 2019-11-29 10:21:25

There is no way for RVO optimization to break the promise of a const, so there's no problem: RVO can be performed.


However, move semantics is affected by the const. It effectively disables move semantics, that is, calls of a T(T&&) constructor or move assignment operator. So in general, don't use const on a return value.

Scott Meyers originally recommended const on return values, for more sane coding.

Then Andrei Alexandrescu, in his Mojo article for DDJ, noted that henceforth, with move semantics, const on return values should better be banned, and Scott's earlier advice ignored.


Now I never bothered to learn the various specialized RVO acronyms, like NRVO and so on. And a main reason is that these changed meaning halfway through, originally having one meaning with some custom functionality in the g++ compiler. The terminology here is just a mess.

So, if my terminology's wrong and I should really have used some other acronym, then please feel free to correct! :-)

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