Passing by const & and returning a temp vs passing by value and returningit

C

Chris Theis

[SNIP]
From your comment below about re-using memory, though, I'm guessing that the
output object is something that you have in reserve, and simply re-use it as
needed. Is that correct? In that case, it sounds like a good solution for
your particular problem. But as a general practice (i.e, outside your
particular case), I still see no real benefit.
[SNIP]

The benefit depends on the application. Doing highly memory intensive
computer graphics or simulations you might resort to your own memory
management anyway, and for some applications you´ll sometimes (have to)
sacrifice elegance for speed.

Chris
 
C

Chris Theis

E. Robert Tisdale said:
Victor Bazarov wrote:
[SNIP]

The compiler could "optimizing one of them away"
if it could inline function(AType).
I'm not sure but I don't think that that is what you meant.
If function(AType) is defined externally
(and not visible to the compiler when it is invoked)
the optimizer will *not* be able to inline it
and the compiler will be obliged to emit code
to pass by value by value (copy somevariable into retval).

I wouldn´t be so quick with that rather general statement. It depends very
much on the inlining capabilities (e.g. inlining during the linking process,
runtime-based inlining, ...) whether this can be optimized or not. But with
most public compilers that are around right now, you´re right.

Chris
 
H

Heinz Ozwirk

Victor Bazarov said:
In the project I'm maintaining I've seen two distinct techniques used for
returning an object from a function. One is

AType function(AType const& arg)
{
AType retval(arg); // or default construction and then..
// some other processing and/or changing 'retval'
return retval;
}

void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); // pass by ref
// some other processing
}

and the other is

AType function(AType retval)
{
// some other processing and changing 'retval'
return retval;
}

void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); // pass by value
// some other processing
}

As you can see the difference is when the variable which later is returned
is constructed. Yes, I realize the difference is really minimal. It is
probably has no effect on performance (and I wouldn't try to speculate one
way or another). The number of objects [copy-] constructed is probably
the same (even in theory, RVO aside). The objects being passed around are
*not* polymorphic. What am I forgetting? Copy-construction is defined
for them and in general no tricks are played. All relatively straight-
forward.

Now, my question is, why would I want to prefer/preserve one way of using
values passed to the function or should I keep both? Do you see any reefs
under the surface, any potential issues with one method or with having
both methods in the same project? Would the optimizer stumble on one of
the methods and not the other, for example?

Generally I prefere the const reference version, but if the very first thing, the function does, is copying its argument, you should thing about passing by value. That might become even more important if the argument can easyly be constructed from some other type.

Example:

std::string ByReference(std::string const& arg)
{
std::string retval(arg);
// ...
return retval;
}

std::string ByValue(std::string arg)
{
// ...
return retval;
}

std::string result;
std::string someString;
char const* someChars;

....

result = ByReference(someString); // 2 copy-ctors
result = ByValue(someString); // 2 copy-ctors
result = ByReference(someChars); // ctor + 2 copy-ctors
result = ByValue(someChars); // ctor + copy-ctor

So there are situations where it is better to pass by value, but I do not recomend it as a general rule.

HTH
Heinz
 
J

Jeff Schwab

Victor said:
In the project I'm maintaining I've seen two distinct techniques used for
returning an object from a function. One is

AType function(AType const& arg)
{
AType retval(arg); // or default construction and then..
// some other processing and/or changing 'retval'
return retval;
}

void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); // pass by ref
// some other processing
}

and the other is

AType function(AType retval)
{
// some other processing and changing 'retval'
return retval;
}

void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); // pass by value
// some other processing
}

As you can see the difference is when the variable which later is returned
is constructed. Yes, I realize the difference is really minimal. It is
probably has no effect on performance (and I wouldn't try to speculate one
way or another). The number of objects [copy-] constructed is probably
the same (even in theory, RVO aside). The objects being passed around are
*not* polymorphic. What am I forgetting? Copy-construction is defined
for them and in general no tricks are played. All relatively straight-
forward.

Now, my question is, why would I want to prefer/preserve one way of using
values passed to the function or should I keep both? Do you see any reefs
under the surface, any potential issues with one method or with having
both methods in the same project? Would the optimizer stumble on one of
the methods and not the other, for example?

I'd appreciate any insight. Thanks!

V


Is it possible that func ever could work without needing a copy of its
argument? If so, you might want to stick with pass-by-reference, so
that you will not be tempted to change the function's signature later.
 
D

Daniel T.

E. Robert Tisdale said:
I would *never* suggest this.


No it isn't. It's just a bad habit.

I'd love to hear your reasoning on why not to return a reference to the
output value (especially since you suggested it below.)
 
E

E. Robert Tisdale

Jeff said:
Victor said:
In the project [that] I'm maintaining,
I've seen two distinct techniques used for returning an object from a function.
One is:

AType function(AType const& arg) {
AType retval(arg); // or default construction and then..
// some other processing and/or changing 'retval'
return retval;
}

and the other is

AType function(AType retval) {
// some other processing and changing 'retval'
return retval;
}

Is it possible that func ever could work
without needing a copy of its argument?
If so, you might want to stick with pass-by-reference,
so that you will not be tempted to change the function's signature later.

What's wrong with changing function's signature later?
All you need to do is recompile the calling function(s).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top