January 2019
Intermediate to advanced
384 pages
11h 50m
English
When we are returning an object from a function by value, actually the compiler should construct a temporary object, fill inside of the function, return it to the caller, and then use assignment to copy the contents of the temporary object into the result object. Pretty inefficient, huh?
Because of that, very early on compilers implemented a simple optimization allowing the called function to fill the result object directly, skipping the unneeded creation of a temporary. There are two cases of that optimization, simple return value optimization (RVO), when we return an unnamed object, and named return value optimization (NRVO), when, true to its name, we return a named object:
T func1(){ ... return T(); // RVO}T func2() ...Read now
Unlock full access