0

We're working with RVO in class to show how we can reduce the number of temporaries created.

I get the basics of it but I'm having difficulty understanding how to combine multiple values to return into one line.

For single temp optimization I was able to understand it fairly easy

const A operator + ( const A &tmp)
{
    A sum;
    sum = this->x + tmp.x;
    return sum;
}

can be reduced to:

const A operator + ( const A &tmp)
{
    return A(this->x + tmp.x);
}

However I'm uncertain how to apply this to a function with multiple values to return. For example:

Vect2D operator - ( const Vect2D &tmp ) const;

Vect2D Vect2D::operator - ( const Vect2D &tmp ) const
{
    Vect2D sum;

    sum.x = this->x - tmp.x;
    sum.y = this->y - tmp.y;

    return ( sum );
};

My process behind it would be:

Vect2D Vect2D::operator - ( const Vect2D &tmp ) const
{
    return Vect2D((this->x - tmp.x), (this->y - tmp.y));
};

which comes back with an error telling me "no argument takes the value (float, float)".

would I have to reorganize the initial

Vect2D operator - ( const Vect2D &tmp ) const;

to take two float arguments within? or am I thinking about this in the wrong way?

Thank you,

E: Thanks to Matt and Jerry for affirming what I thought I needed to do with the double float operator.

  • I don't see what your exact question is, can you post a piece of code in which there is too many temporaries created and needs fixing? – M.M Oct 30 '14 at 23:01
  • (N)RVO is an exception to the as-if rule, which generally governs allowed compiler-optimizations. It seems you are trying to relieve the compiler from (most of the drudgery in) having to use that rule. – Deduplicator Oct 30 '14 at 23:01
  • The line `Vect2D((this->x - tmp.x), (this->y - tmp.y));` attempts to create a `Vect2D` using a constructor taking two arguments. Your error message indicates there is no such constructor so perhaps you should add one! – M.M Oct 30 '14 at 23:02
  • For your `return Vect2D((this->x - tmp.x), (this->y - tmp.y));` to work, you need a `Vect2D::Vect2D(float, float)`, to construct the return object from the two values. – Jerry Coffin Oct 30 '14 at 23:02
  • 1
    What does RVO have to do with this question? – Benjamin Lindley Oct 30 '14 at 23:06
  • I would expect any decent compiler, with a bit of optimisation enabled, to produce EXACTLY the same code for both your variants of `Vect2D::operator-` - so I don't really see the point in working on this. Either the constructor is inlined (and then made into nothing, unless it actually does something more than setting the `x` and `y` members). – Mats Petersson Oct 30 '14 at 23:14
  • The rule for RVO / NRVO is: if all return paths are r-values or if all return paths are the same local variable, (N)RVO is viable. – Billy ONeal Oct 30 '14 at 23:31

1 Answers1

0

The solution to my issue was not understanding that operators require the Friend function in order to have two arguments. While not entirely RVO the solution does reduce the number of temps created by having a return with both variables in it.

Thanks again to Matt and Jerry for getting me started on the process.