如何检查C ++复制椭圆 [英] How to check for C++ copy ellision

查看:150
本文介绍了如何检查C ++复制椭圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在复制ellision 在C + +和我已经看到在升压库中的意见。这是很有吸引力的,因为我喜欢我的功能看起来像

I ran across this article on copy ellision in C++ and I've seen comments about it in the boost library. This is appealing, as I prefer my functions to look like

verylargereturntype DoSomething(...)

而不是

void DoSomething(..., verylargereturntype& retval)



So, I have two questions about this


  1. Google几乎没有这方面的文件,这是怎么回事?

  2. 如何检查优化实际上是发生的?我认为它涉及到看装配,但只是说,这不是我的强大西装。如果任何人都能给出一个非常基本的例子,说明成功的椭圆形是什么样子,这将是非常有用的。

复制椭圆只是为了美化的东西,但如果我可以保证它的工作,它听起来很有用。

I won't be using copy ellision just to prettify things, but if I can be guaranteed that it works, it sounds pretty useful.

推荐答案

是一种非常常用的优化,因为:

I think this is a very commonly applied optimization because:


  1. 编译器不难执行


如果你只是好奇,在你的拷贝构造函数中放一个调试 printf()

If you're just curious, put a debug printf() in your copy constructor:

class foo {
public:
    foo(): x(0) {};

    foo(int x_) : x( x_) {};

    foo( foo const& other) : x( other.x) {
        printf( "copied a foo\n");
    };

    static foo foobar() {
        foo tmp( 2);

        return tmp;
    }


private:
    int x;
};



int main()
{
    foo myFoo;

    myFoo = foo::foobar();

    return 0;
}

当我运行未优化的构建时打印出复制foo当我构建优化。

Prints out "copied a foo" when I run an unoptimmized build, but nothing when I build optimized.

这篇关于如何检查C ++复制椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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