From: Martin T. on
Hi all.

When writing new C++ code, what is the point of passing any (input)
parameter by value when passing by const reference will just work as
well? (Even and especially PODs, I would not do it with a complex type
anyway.)
(Given that in 90% of the code you will never want to modify the
parameter anyway.)

br,
Martin

[EXAMPLE_CODE]
class foo {
};

void f_val_int(int);
void f_ref_int(const int&);
void f_val_foo(foo);
void f_ref_foo(const foo&);

int main(int,)
{
double d = 1.2;
f_val_int(d);
f_ref_int(d);
f_val_int(1.4);
f_ref_int(1.5);

foo x;
f_val_foo(x);
f_ref_foo(x);
f_val_foo(foo());
f_ref_foo(foo());

return 0;
};

void f_val_int(const int x) {
int y = x;
y++;
}

void f_ref_int(const int& x) {
int y = x;
y++;
}

void f_val_foo(const foo f) {
foo ff = f;
ff;
}

void f_ref_foo(const foo& f) {
foo ff = f;
ff;
}
[/EXAMPLE_CODE]

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jim Langston on

"Martin T." <0xCDCDCDCD(a)gmx.at> wrote in message
news:g4ferd$sji$1(a)registered.motzarella.org...
> Hi all.
>
> When writing new C++ code, what is the point of passing any (input)
> parameter by value when passing by const reference will just work as
> well? (Even and especially PODs, I would not do it with a complex type
> anyway.)
> (Given that in 90% of the code you will never want to modify the
> parameter anyway.)

1. Passing an interger by value is usually faster than passing an integer by
reference.
2. Passing a paramater by value allows modification of the paramater as a
local variable I.E.

void Reverse( const char* CString, int length )
{
while ( --length )
{
std::cout << CString[length];
}
}

3. A lot of existing C code passes by value.
4. Sometimes it is clearer to pass by value rather than by reference. I
once toyed with changing all pointer paramters in a program to references
and some code got jsut downright ugly.
5. Choice
6+ Reasons I haven't thought of


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Eric Johnson on
On Jul 2, 10:58 am, "Martin T." <0xCDCDC...(a)gmx.at> wrote:
> Hi all.
>
> When writing new C++ code, what is the point of passing any (input)
> parameter by value when passing by const reference will just work as
> well? (Even and especially PODs, I would not do it with a complex type
> anyway.)

<snip>

In theory, passing a small type (int, char, float, etc) by value will
be more efficient than passing by const reference. In your example
function "f_ref_int", the address of the int gets passed to the
function and then the value of "x" has to be fetched, whereas in
f_val_int, the value of "x" is passed directly.

In practice, it's hard to say if one would see much performance
difference without taking some measurements.

-Eric


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Hakusa on
> ...what is the point of passing any (input)
> parameter by value when passing by const reference will just work as
> well? (Even and especially PODs, I would not do it with a complex type
> anyway.)

Well, for the POD's, or any complex data type (I don't know what a POD
is), a copy constructor should make passing by const reference and
value irrelevant...well, short of efficiency.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: mtlung on
On 7月3日, 上午1時58分, "Martin T." <0xCDCDC...(a)gmx.at> wrote:
> Hi all.
>
> When writing new C++ code, what is the point of passing any (input)
> parameter by value when passing by const reference will just work as
> well? (Even and especially PODs, I would not do it with a complex type
> anyway.)
> (Given that in 90% of the code you will never want to modify the
> parameter anyway.)
>
> br,
> Martin
>
> [EXAMPLE_CODE]
> class foo {
>
> };
>
> void f_val_int(int);
> void f_ref_int(const int&);
> void f_val_foo(foo);
> void f_ref_foo(const foo&);
>
> int main(int,)
> {
> double d = 1.2;
> f_val_int(d);
> f_ref_int(d);
> f_val_int(1.4);
> f_ref_int(1.5);
>
> foo x;
> f_val_foo(x);
> f_ref_foo(x);
> f_val_foo(foo());
> f_ref_foo(foo());
>
> return 0;
>
> };
>
> void f_val_int(const int x) {
> int y = x;
> y++;
>
> }
>
> void f_ref_int(const int& x) {
> int y = x;
> y++;
>
> }
>
> void f_val_foo(const foo f) {
> foo ff = f;
> ff;
>
> }
>
> void f_ref_foo(const foo& f) {
> foo ff = f;
> ff;}
>
> [/EXAMPLE_CODE]

{ Edits: quoted clc++m banner removed, since it's available at the end of this
article (and indeed, every clc++m article). There's no need to quote it. -mod }

It is because passing by reference/pointer will introduce the alias
problem, which hinder the compiler to perform a numbers of
optimizations.
Therefore, in a performance point of view, primitive types should pass
by value while large data structure should pass by reference/pointer.
More information on the aliasing problem:
http://www.ddj.com/cpp/184404273;jsessionid=FAZXOZJKL5K24QSNDLPCKHSCJUNN2JVN?_requestid=161084

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]