Other than return-value optimization, C++ doesn't allow optimizations that change visible side effects of the program. (What are copy elision and return value optimization?). e.g. a cout <<
in a constructor might not get called, or in more recent C++ versions is guaranteed not to get called in some cases where you'd naively expect it (and where earlier C++ revisions would make that side effect happen).
This isn't that, so the call to getInput()
definitely has to happen, even if its return value ends up not being used (because it inlines and optimizes away void function(const std::string& input) {}
). The fact that getInput()
's return value is being passed to an empty function makes zero difference in the call to getInput()
itself.
As a general rule, only visible side effects in constructors / destructors are ever in danger of being optimized away. It's normally a bad idea to put visible side effects in constructors, except for side effects on the object being constructed. Except for debug logging where you want it to not log depending on optimization.