Will the c++ compiler optimize away unused return value?

后端 未结 7 579
灰色年华
灰色年华 2021-01-18 19:24

If I have a function that returns an object, but this return value is never used by the caller, will the compiler optimize away the copy? (Possibly an always/sometimes/never

7条回答
  •  忘掉有多难
    2021-01-18 20:05

    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.

    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).

提交回复
热议问题