RVO(返回值优化)是否适用于所有对象? [英] Is RVO (Return Value Optimization) applicable for all objects?

查看:226
本文介绍了RVO(返回值优化)是否适用于所有对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是RVO( <返回值优化 )保证或适用于所有对象和情况C ++编译器(特别是GCC)?

Is RVO (Return Value Optimization) guaranteed or applicable for all objects and situations in C++ compilers (specially GCC)?

如果回答是否,那么类/对象的优化条件是什么?如何强制或鼓励编译器对特定的返回值执行RVO?

If answer is "no", what are the conditions of this optimization for a class/object? How can I force or encourage the compiler to do a RVO on a specific returned value?

推荐答案

始终应用,不能普遍应用的是命名返回值优化。基本上,为了进行优化,编译器必须知道要在对象被构造的地方返回什么对象。

Return Value Optimization can always be applied, what cannot be universally applied is Named Return Value Optimization. Basically, for the optimization to take place, the compiler must know what object is going to be returned at the place where the object is constructed.

在RVO(返回一个临时数据)的情况下,该条件被简单地满足:该对象在return语句中构造,并且它返回。

In the case of RVO (where a temporary is returned) that condition is trivially met: the object is constructed in the return statement, and well, it is returned.

在NRVO的情况下,您必须分析代码以了解编译器是否知道该信息。如果函数的分析很简单,编译器会优化它(单个返回语句不包含条件语句,例如;同一对象的多个返回语句;多个返回语句如 T f(){if(condition){T r; return r;} else {T r2; return r2;}} 其中编译器知道 r r2 将会被返回...)

In the case of NRVO, you would have to analyze the code to understand whether the compiler can know or not that information. If the analysis of the function is simple, chances are that the compiler will optimize it (single return statement that does not contain a conditional, for example; multiple return statements of the same object; multiple return statements like T f() { if (condition) { T r; return r; } else { T r2; return r2; } } where the compiler knows that r or r2 will be returned...)

请注意,只是假设在简单的情况下优化,具体来说,维基百科的示例实际上可以通过一个聪明的编译器优化:

Note that you can only assume the optimization in simple cases, specifically, the example in wikipedia could actually be optimized by a smart enough compiler:

std::string f( bool x ) {
   std::string a("a"), b("b");
   if ( x ) return a; 
   else return b;
}

可由编译器重写为:

std::string f( bool x ) {
   if ( x ) {
      std::string a("a"), b("b");
      return a;
   } else {
      std::string a("a"), b("b");
      return b;
   }
}

编译器可以知道,第一个分支 a 将被替换为返回的对象,在第二个分支中同样适用于 b 。但我不会指望。如果代码很复杂,则假设编译器无法生成优化。

And the compiler can know at this time that in the first branch a is to be constructed in place of the returned object, and in the second branch the same applies to b. But I would not count on that. If the code is complex, assume that the compiler will not be able to produce the optimization.

EDIT :有一种情况没有明确提到,编译器是不允许的(在大多数情况下,即使它被允许,它不可能做到)优化离开从函数的参数到return语句的副本:

EDIT: There is one case that I have not mentioned explicitly, the compiler is not allowed (in most cases even if it was allowed, it could not possibly do it) to optimize away the copy from an argument to the function to the return statement:

T f( T value ) { return value; } // Cannot be optimized away --but can be converted into
                                 // a move operation if available.

这篇关于RVO(返回值优化)是否适用于所有对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆