[LC++]Using the "return value optimization"

Mark Phillips mark at austrics.com.au
Mon Aug 13 20:31:04 UTC 2001


Vincent Penquerc'h wrote:
> 
> >       return int(doSomething(***here***, parameter));
> >
> > Unfortunately no such syntax exists (to my knowledge).  Is
> > there any way around my problem?
> 
> Well, try it, and you'll see it actually works, if your
> doSomething function returns the int:
> 
> int doSomething(int const& parameter);
> ...
> return int(doSomething(parameter));

Yes, but the problem is that my "doSomething" function is void.
It "returns the value" using a non-constant reference in the
argument list.  Ie 

  void doSomething(int& i, int const& parameter);

This is a very efficient way of returning information.  What
I would like to be able to do, is to define a variant form
of doSomething in terms of the original one.  Ie have
the overloaded function:

int doSomething(int const& parameter) {
  int temp;
  doSomething(temp, parameter);
  return temp;
}

Now this works, but it isn't very efficient.  I was hoping
to use return value optimization to make it more efficient.
But it looks like c++ syntax won't allow this.  I realize
that even with return value optimization, it won't be as
efficient as the first version.  But I would like to make
it as efficient as possible.  In any case, from what you 
said later in your email, return value optimization wouldn't 
help me anyway.

What I would really like it to do, is for return value
optimization to take the following code:

  int a=doSomething(parameter);

and convert it into:

  int a;
  doSomething(a, parameter);

It would seem like this is not possible.

Cheers,

Mark.



More information about the tuxCPProgramming mailing list