Now that most code bases are using modern (post C++11) compilers, it’s common to encounter the move semantic judiciously used throughout. Implicitly or explicitly. Which brings us to return value optimization (RVO), which the standard refers to as copy elision [Copying and moving class objects / 12.8.31].
“ … 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.”
In brief, RVO is an optimization (compiler isn’t required to, but the fallback isn’t bad either) where the return value is constructed directly into the returned value location. Meaning, you avoid all copying. I say all, since no conversion can take place since for the RVO to considered by the compiler, the return type and the returned to object must be the same type. Basically, this:
Continue reading “Return value optimization (copy elision)“