标准允许优化(6.3.3)。The standard allows the optimization (6.3.3). 在我的副本中,6.3是复合语句或块。并且只有一个 段落。 IOW,没有6.3.3? 问候 - jb (如果你想通过电子邮件回复,用x替换y)In my copy, 6.3 is "Compound statement or block" and only has a singleparagraph. IOW, there is no 6.3.3?regards--jb(replace y with x if you want to reply by e-mail) 正如杰夫所说,在上面例如,允许编译器优化 远离不必要的本地(在func1()),临时和相应的 复制构造函数调用。如果func1()定义如下: test func1(int r) { test t; t.m_strings.push_back(" lala"); 返回t; } with相同的main(),我肯定会期待一个体面的编译器只有一个构造和 破坏。然而,递归可能会使某些事情复杂化。您可以添加几个ctors进行测试(默认和复制)和 a dtor cout a message and see。 考虑一下: int main() { test t; //做点什么...... t = func1(3); } 在这种情况下运气不大:t将首先构建,然后在至少一个 类测试的对象将被创建为func1(),然后operator = 将被调用。 当我关心性能时,我通常将func1()定义为 void func1(int r,test& t){...} 如果功能表示法很重要,我会考虑使用代理 (并且相应地容纳实际对象),但我不会从函数返回 真正的大对象。 /> DenisAs Jeff said, in the above example the compiler is allowed to optimiseaway unnecessary locals (in func1()), temporaries and the correspondingcopy constructor calls. If func1() were defined like this:test func1 (int r){test t;t.m_strings.push_back ("lala");return t;}with the same main(), I would definitely expect just one construction anddestruction from a decent compiler. The recursion, however, may complicatethings a bit. You can add a couple of ctors to test (default and copy) anda dtor that cout a message and see.Consider this too:int main(){test t;//do something to t ...t = func1 (3);}Not much luck in this case: t will be constructed first, then at least oneobject of class test will be created as a result of func1(), then operator =will be called.When I care about performance, I usually define func1() asvoid func1 (int r, test& t) {...}If the functional notation were important, I would consider using proxies(and accomodate the "actual" objects acordingly), but I would not returnreally big objects from a function.Denis 这篇关于问:通过几个函数调用返回值优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 13:46