O'Reilly logo

Efficient C++ Performance Programming Techniques by David Mayhew, Dov Bulka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Computational Constructors

When the compiler fails to apply the RVO, you can give it a gentle nudge in the form of the computational constructor (originally attributed to J. Shopiro [Car92, Lip96I].) Our compiler did not apply the RVO to Version 1:

Complex operator+ (const Complex& a,  const Complex& b)
  // operator+ version 1.
{
    Complex retVal;

    retVal.real = a.real + b.real;
    retVal.imag = a.imag + b.imag;

    return retVal;
}

This implementation created a default Complex object and deferred setting its member fields. Later it filled in the member data with information supplied by the input objects. The production of the Complex retVal object is spread over multiple distinct steps. The computational constructor collapses these steps into a single ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required