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

C++ use of references for int type?

10 views
Skip to first unread message

nvangogh

unread,
Apr 12, 2013, 8:56:36 AM4/12/13
to
I seem to see examples of code where references are used in functions
for class types / data structures. I have not seen a reference used in a
function for an int type. I thought that the reason to use references in
function calls especially might be to avoid the overhead of a call by
value. If a data structure is large, this might be significant. Whereas
a 'simple in built' int type is of a fixed quantity and the effect of
passing such a value by copy is always going to be trivial and not
affect program performance.

would you agree that this reasoning is correct and therefore int type
references are not necessary for function calls for this specific
reason?

Ike Naar

unread,
Apr 12, 2013, 10:04:15 AM4/12/13
to
On 2013-04-12, nvangogh <nvan...@invalid.net> wrote:
> I seem to see examples of code where references are used in functions
> for class types / data structures. I have not seen a reference used in a
> function for an int type. I thought that the reason to use references in
> function calls especially might be to avoid the overhead of a call by
> value. If a data structure is large, this might be significant. Whereas
> a 'simple in built' int type is of a fixed quantity and the effect of
> passing such a value by copy is always going to be trivial and not
> affect program performance.

That is one reason. Another reason to use references is to allow
a function to modify its arguments. An example of such a function
is std::swap,

#include <algorithm> // C++2011: #include <utility>
#include <cassert>

int main()
{
int x=1, y=2;
assert(x==1 && y==2);
std::swap(x,y);
assert(x==2 && y==1);
return 0;
}

Francis Glassborow

unread,
Apr 16, 2013, 7:43:43 PM4/16/13
to
You need to distinguish between passing by const reference and passing
by reference. When you pass by reference the original is available for
modification, but when passing by value this is not the case.

It is normal to pass large (substantially larger than a pointer in
storage requirement) values by const reference and small objects get
passed by value. Indeed it is more efficient at the micro-level to pass
by value as long as the copying cost is low.

If you want to modify the original (sometimes called an out parameter)
it is necessary to either pass the address explicitly (by using a
pointer) or by using a plain pointer. The later method is preferred in C++.

I think a compiler is allowed to optimise a pass by value into a const
reference as long as the resulting code will have the same visible
results (this is allowed under what id called the 'as if' rule (the
compiler can apply any optimisation that will not change the
output/behaviour (in the widest sense) of the program.

Francis

Francis Glassborow

unread,
Apr 16, 2013, 7:54:34 PM4/16/13
to
On 17/04/2013 00:43, Francis Glassborow wrote:
> On 12/04/2013 13:56, nvangogh wrote:
>> I seem to see examples of code where references are used in functions
>> for class types / data structures. I have not seen a reference used in a
>> function for an int type. I thought that the reason to use references in
>> function calls especially might be to avoid the overhead of a call by
>> value. If a data structure is large, this might be significant. Whereas
>> a 'simple in built' int type is of a fixed quantity and the effect of
>> passing such a value by copy is always going to be trivial and not
>> affect program performance.
>>
>> would you agree that this reasoning is correct and therefore int type
>> references are not necessary for function calls for this specific reason?
>
>
> You need to distinguish between passing by const reference and passing
> by reference. When you pass by reference the original is available for
> modification, but when passing by value this is not the case.
>
> It is normal to pass large (substantially larger than a pointer in
> storage requirement) values by const reference and small objects get
> passed by value. Indeed it is more efficient at the micro-level to pass
> by value as long as the copying cost is low.
>
> If you want to modify the original (sometimes called an out parameter)
> it is necessary to either pass the address explicitly (by using a
> pointer) or by using a plain pointer. The later method is preferred in C++.

Sorry, that should have been 'plain reference'.

Francis
0 new messages