Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy vs. const reference function argument

0 views
Skip to first unread message

Przemyslaw Koprowski

unread,
Mar 25, 2008, 5:32:55 PM3/25/08
to
Hi all,

Is there any difference between the following:

foo someFunction(const foo& A)
{
foo B(A);
// do sth. to B
return B;
}

and

foo someFunction(foo A)
{
// do sth. to A
return A;
}

I think there is none (a short test on gcc, with all
optimalizations on, seems to support this), but if
there is any I would like to know.

Is any of these two forms preferable? Why?

TIA,
Przemek


P.S.
The context for the question (if is of any importance here)
is mostly operator overloading. Say:
qForm operator + (qForm A, const qForm &B)
{
return A += B;
}
Assuming that qForm::operator += is already defined.

Ian Collins

unread,
Mar 25, 2008, 3:55:44 PM3/25/08
to
Przemyslaw Koprowski wrote:
> Hi all,
>
> Is there any difference between the following:
>
> foo someFunction(const foo& A)
> {
> foo B(A);
> // do sth. to B
> return B;
> }
>
> and
>
> foo someFunction(foo A)
> {
> // do sth. to A
> return A;
> }
>
> I think there is none (a short test on gcc, with all
> optimalizations on, seems to support this), but if
> there is any I would like to know.
>
> Is any of these two forms preferable? Why?
>
I all depends how expensive a foo is to copy. The idiomatic form would
be cost&. If foo is small, it may be cheaper to pass by value, but that
would change if ever foo changed.

--
Ian Collins.

Przemyslaw Koprowski

unread,
Mar 25, 2008, 5:54:40 PM3/25/08
to
On Wed, 26 Mar 2008 08:55:44 +1300, Ian Collins wrote:
> I all depends how expensive a foo is to copy. The idiomatic form would
> be cost&. If foo is small, it may be cheaper to pass by value, but that
> would change if ever foo changed.
>
Why? After all, the foo object is being copied in both cases.
Either explicitely when constructing B:

>> foo someFunction(const foo& A)
>> {
>> foo B(A);
>> // do sth. to B
>> return B;
>> }

or implicitely when passing an argument by value:


>> foo someFunction(foo A)
>> {
>> // do sth. to A
>> return A;
>> }

That's why I don't expect any difference. Could you ellaborate
why size of foo shall matter here?

Przemek

Victor Bazarov

unread,
Mar 25, 2008, 4:28:02 PM3/25/08
to

I think a common implementation of references for the purposes of
passing them as arguments is a pointer. IOW, behind the scenes the
compiler generates the code that passes the address of the object
when you write pass by reference. Unless the function is inlined,
passing an address requires for the item to be in memory. For some
cases it would actually be better to pass the argument itself if it
can be kept in a processor register (instead of storing it in memory
even if it's just in the stack). A long time ago, in a different
life, I timed calling an empty function that takes a double as its
argument, another that takes a reference to a double, and yet
another that takes a pointer to a double. With certain optimization
options the compiler generated the fastest code when the double was
passed by value. With other options the difference was not serious
enough to mention. YMMV

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Ian Collins

unread,
Mar 25, 2008, 4:42:09 PM3/25/08
to
Przemyslaw Koprowski wrote:
> On Wed, 26 Mar 2008 08:55:44 +1300, Ian Collins wrote:
>> I all depends how expensive a foo is to copy. The idiomatic form would
>> be cost&. If foo is small, it may be cheaper to pass by value, but that
>> would change if ever foo changed.
>>
> Why? After all, the foo object is being copied in both cases.

I didn't spot the copy.

> Either explicitely when constructing B:
>>> foo someFunction(const foo& A)
>>> {
>>> foo B(A);
>>> // do sth. to B
>>> return B;
>>> }
>
> or implicitely when passing an argument by value:
>>> foo someFunction(foo A)
>>> {
>>> // do sth. to A
>>> return A;
>>> }
>
> That's why I don't expect any difference. Could you ellaborate
> why size of foo shall matter here?
>

In addition to Victors comments, on some machines (notably RISC machines
with register wheels) several registers are used to pass parameters. One
example is Sun CC on Sparc, where it is faster to pass a std::string by
value than by reference.

--
Ian Collins.

Andrey Tarasevich

unread,
Mar 25, 2008, 5:29:59 PM3/25/08
to
Przemyslaw Koprowski wrote:
> Hi all,
>
> Is there any difference between the following:
>
> foo someFunction(const foo& A)
> {
> foo B(A);
> // do sth. to B
> return B;
> }
>
> and
>
> foo someFunction(foo A)
> {
> // do sth. to A
> return A;
> }
>
> I think there is none (a short test on gcc, with all
> optimalizations on, seems to support this), but if
> there is any I would like to know.

Semantically, there's a difference. When you pass by reference, the
compiler has to preserve the lvalue identity of the argument. For example:

1) Built-in unary '&' applied to the parameter has to return the address
of the actual argument

void foo(const int& a, const int& b) {
assert(&a == &b);
}

// The above 'assert' has to hold if called as
int i;
foo(i, i);

2) A non-constant argument can be legally modified through const
reference by using 'const_cast'

void foo(const int& a) {
const_cast<int&>(a) = 42;
}

// The above modification is legal if called as
int i;
foo(i);
assert(i == 42);
// i.e. this assertion has to hold

3) Possible aliasing

int x = 0;

void foo(const int& a) {
x = 42;
assert(a == 42);
}

// The above 'assert' has to hold if called as
foo(x);

Which means that the compiler can't simply treat const reference
parameters as being equivalent to value parameters. They are not.

Of course, a very smart compiler can recognize these situations and
stick to the strict semantics when they occur, while using const
references and copied values interchangeably in all other cases. This is
not simple though in general case.

> Is any of these two forms preferable? Why?

In situations when you can use either, it might be more optimal to pass
small objects by value and large objects by reference.

--
Best regards,
Andrey Tarasevich

aceh...@gmail.com

unread,
Mar 25, 2008, 5:44:52 PM3/25/08
to
On Mar 25, 2:32 pm, Przemyslaw Koprowski <o...@siggraph.pkoprowski>
wrote:

> Hi all,
>
> Is there any difference between the following:
>
> foo someFunction(const foo& A)
> {
> foo B(A);
> // do sth. to B
> return B;
>
> }
>
> and
>
> foo someFunction(foo A)
> {
> // do sth. to A
> return A;
>
> }

A number of years ago, Andrei Alexandrescu was arguing on comp.lang.c+
+.moderated that pass-by-value would give the compiler more
oportunities for optimizations. He was recommending this form of
operator=:

Foo & Foo::operator= (Foo temp)
{
this->swap(temp);
return *this;
}

But you haven't seen any differences in your testing... :)

Ali

Hans Bos

unread,
Mar 26, 2008, 6:29:50 AM3/26/08
to
"Przemyslaw Koprowski" <o...@siggraph.pkoprowski> schreef in bericht
news:pan.2008.03.25....@siggraph.pkoprowski...

> Hi all,
>
> Is there any difference between the following:
>
> foo someFunction(const foo& A)
> {
> foo B(A);
> // do sth. to B
> return B;
> }
>
> and
>
> foo someFunction(foo A)
> {
> // do sth. to A
> return A;
> }
>

I think the second form can be more efficient.
If you don't pass directly a foo object but something that can be converted
in a foo the first form will create two foo objects.

E.g.
If someFunction(const std::string &A) is called as someFunction("..."), the
literal string is converted to a std::string. When you make the copy, the
string is created twice.
In std::string someFunction(std::string A), there will be only one string.

So I would recommend the second option. It is a more general solution and
doesn't hurt performance.

Hans.

0 new messages