且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c++ 编译器会优化掉未使用的返回值吗?

更新时间:2023-11-12 13:32:40

如果 ReturnValue 类有一个非平凡的复制构造函数,编译器不能消除对复制构造函数的调用——它是由它所在的语言规定的调用.

If the ReturnValue class has a non-trivial copy constructor, the compiler must not eliminate the call to the copy constructor - it is mandated by the language that it is invoked.

如果复制构造函数是内联的,编译器可能能够内联调用,这反过来可能会导致删除其大部分代码(也取决于 FunctionThatAltersMembersAndNeverFails 是否内联).

If the copy constructor is inline, the compiler might be able to inline the call, which in turn might cause a elimination of much of its code (also depending on whether FunctionThatAltersMembersAndNeverFails is inline).