且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++编译器可以为const返回值执行RVO吗?

更新时间:2022-05-20 01:45:10

RVO优化无法打破 const 的承诺,因此没有问题:可以执行RVO。

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

但是,移动语义 const $ c影响$ c>。它有效地禁用了移动语义,即 T(T&)构造函数或移动赋值运算符的调用。因此,通常不要在返回值上使用 const

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最初建议 const 返回值,以进行更合理的编码。

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

然后,Andrei Alexandrescu在其关于DDJ的Mojo文章中指出,此后,移动语义,***禁止返回值使用 const ,而忽略Scott的早期建议。

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.

现在,我再也不用花时间学习各种专用RVO缩写,例如NRVO等。主要原因是这些含义改变了到一半,最初在g ++编译器中具有某种自定义功能的含义。

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! :-)