Quick A: Yes. That's kind of the point, actually.
This weekend on StackOverflow:
Return value optimizations and side-effects
Return value optimization (RVO) is an optimization technique involving copy elision, which eliminates the temporary object created to hold a function's return value in certain situations. I understand the benefit of RVO in general, but I have a couple of questions.
The standard says the following about it in §12.8, paragraph 32 of this working draft (emphasis mine).
When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.
It then lists a number of criteria when the implementation may perform this optimization.
I have a couple of questions regarding this potential optimization:
- I am used to optimizations being constrained such that they cannot change observable behaviour. This restriction does not seem to apply to RVO. Do I ever need to worry about the side effects mentioned in the standard? Do corner cases exist where this might cause trouble?
- What do I as a programmer need to do (or not do) to allow this optimization to be performed? For example, does the following prohibit the use of copy elision (due to the move):
std::vector<double> foo(int bar){ std::vector<double> quux(bar,0); return std::move(quux); }
Add a Comment
Comments are closed.