G++编译器的返回值优化

本文介绍了G++编译器的返回值优化,即C++标准允许编译器在某些情况下省略创建临时对象。通过使用-fno-elide-constructors选项,可以禁用这一优化,强制调用复制构造函数。此选项在C++17中仍然会影响trivial成员函数。文章还包含了一个实验,通过比较加不加该编译参数的区别来观察Obj类对象的构造过程。

摘自Options Controlling C++ Dialect, Chapter 3.5, Using the GNU Compiler Collection – For GCC version 8.2.0

-fno-elide-constructors
The C++ standard allows an implementation to omit creating a temporary that 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. This option also causes G++ to call trivial member functions which otherwise would be expanded inline.
In C++17, the compiler is required to omit these temporaries, but this option still affects trivial member functions.

译文
  C++标准允许编译器有如下实现:如果一个临时对象仅仅被用来初始化另一个同类型的对象,那么则可以省掉这种临时对象(而直接构造目标对象)。加入-fno-elide-constructors编译参数后可关闭优化,即强制G++进行复制构造。(译注:后面一句不懂)
  C++17标准要求编译器省略临时对象(译注:意味着优化不可被关闭?),但这个参数仍会影响trivial成员函数。

实验
  已知Obj类。定义fun()函数,返回

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值