G++的命名返回值优化 -- NRVO

        今天在运行C++ primer中一段程序的时候,没有打印复制构造函数的信息,代码如下:

        

class Exmpl {
public:
        Exmpl() {cout << "Exmple()" << endl;}
        Exmpl(const Exmpl&) {cout << "Exmple(const Exmpl&)" << endl;}
        Exmpl & operator = (const Exmpl &rhs)
        {
                cout << "operator = (const Exmple&rhs)" << endl;
                return *this;
        }

        ~Exmpl()
        {
                cout << "~Exmpl()" << endl;
        }
};

void func1(Exmpl obj)
{
}

void func2(Exmpl &obj)
{
}

Exmpl func3()
{
        Exmpl obj;
        return obj;
}

int main(int argc, char **argv)
{
        Exmpl eobj;
        func1(eobj);
        func2(eobj);

        eobj = func3();
}


         按照我的分析,输出结果如下:

         

Exmple()
Exmple(const Exmpl&)
~Exmpl()
Exmple()
Exmple(const Exmpl&)
~Exmpl()
operator = (const Exmple&rhs)
~Exmpl()
~Exmpl()


         可是我电脑上的输出如下(我使用的是g++ 4.0.2)

       

Exmple()
Exmple(const Exmpl&)
~Exmpl()
Exmple()
operator = (const Exmple&rhs)
~Exmpl()
~Exmpl()


        查阅资料,原来是g++默认启用了命名返回值优化,named return value optimizatio - NRVO

        加上-fno-elide-constructors后,就可以关掉NRVO了。

        g++的手册说:

-fno-elide-constructors
          The C++ standard allows an implementation to omit creating a
          temporary which is only used to initialize another object of the
          same type.  Specifying this option disables that optimization, and
          forces G++ to call the copy constructor in all cases. 


        我在Visual Studio 2005下运行上面的程序,输出和没有加-fno-elide-constructors的g++编译出来的程序一样。这说明Visual Studio 2005默认是不会开启该优化选项的。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值