摘自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()函数,返回