复制构造函数与返回值优化 [英] Copy constructor vs. return value optimization

查看:164
本文介绍了复制构造函数与返回值优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一个问题,似乎一个简单的返回值函数总是将其 return 参数复制到从中赋值的变量中。

In a previous question, it appeared that a plain return-by-value function always copies its return argument into the variable being assigned from it.

这是标准要求的,还是可以通过在函数体内构造assign to变量来优化函数?

Is this required by the standard, or can the function be optimized by constructing the 'assigned to' variable even within the function body?

struct C { int i; double d; };

C f( int i, int d ) {
    return C(i,d); // construct _and_ copy-construct?
}

int main() {
    C c = f( 1, 2 ); 
}


推荐答案

在这里复制省略:


  • 构造本地临时,将其复制构造为返回值,并将返回值复制到本地C。

  • 构建本地临时文件,并将其复制到c中。 OR

  • 使用参数i,d构建c

这篇关于复制构造函数与返回值优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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