Elision / Return Value Optimization (RVO)
In certain cases, compiler can skip calling copy / more constructers, instead writing the object’s value s directly into its final location.
Consider the example below:
Rational makeRational() {return Rational{1, 5}};
Rational r = makeRational();- In
g++ std=c++14, neither move nor copy gets called. Elision is used
Another example
void doMath(Rational r) {
...
}
doMath(makeRational());Here, {1,5} is directly written into r.
Note: Elision is possible, even if it changes program output.
- Not expected to know all possible cases, just that it’s possible.
Disable with the flag -fno-elide-constructors (slows down program though)
Copy Elision
https://en.wikipedia.org/wiki/Copy_elision https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization
In the context of the C++ programming language, Return Value Optimization (RVO) is a compiler optimization that involves eliminating the temporary object created to hold a function’s return value.