为什么Visual C ++不对最小的代码执行返回值优化? [英] Why is Visual C++ not performing return-value optimization on the most trivial code?

查看:159
本文介绍了为什么Visual C ++不对最小的代码执行返回值优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual C ++不执行返回值优化?

Does Visual C++ not perform return-value optimization?

#include <cstdio>
struct Foo { ~Foo() { printf("Destructing...\n"); } };
Foo foo() { return Foo(); }
int main() { foo(); }

我编译并运行它:

cl /O2 test.cpp
test.exe

它打印:


破坏...

破坏...

Destructing...
Destructing...

为什么不执行RVO?

推荐答案

测试:

#include <iostream>
struct Foo { 
    Foo(Foo const &r) { std::cout << "Copying...\n"; }
    ~Foo() { std::cout << "Destructing...\n"; }
    Foo() {}
};

Foo foo() { return Foo(); }

int main() { Foo f = foo(); }

...我得到的输出是:

...the output I get is:

Destructing...

复制构造函数,并且只有一个析构函数。

No invocation of the copy constructor, and only one of the destructor.

这篇关于为什么Visual C ++不对最小的代码执行返回值优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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