469,609 Members | 1,574 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Is a const parameter generally optimized to a reference?

Hi,

The point of using const on a parameter to a function should be to let
your compiler know that the parameter shouldn't be modified during your
program. This allows you to keep your code safe and bug-free.

Now, it also occurs to me that a const something-or-other could be
passed as a reference (since it's guaranteed not to change) , or that
the address of the object could be passed rather than the whole thing in
the case of a large object. Is this generally done by the compiler? Is
this one of the good reasons for using const?
Jul 23 '05 #1
7 1243
Richard Cavell wrote:
Hi,

The point of using const on a parameter to a function should be to let
your compiler know that the parameter shouldn't be modified during your
program. This allows you to keep your code safe and bug-free.

Now, it also occurs to me that a const something-or-other could be
passed as a reference (since it's guaranteed not to change) , or that
the address of the object could be passed rather than the whole thing in
the case of a large object. Is this generally done by the compiler?
No. It must be done by you. If you want to pass by reference, just pass by
reference.
Is this one of the good reasons for using const?


It is _the_ reason for using const parameters for me. I don't use const for
parameters passed by value, since those can't modify the passed value
anyway. I often do things like:

void foo(int count)
{
while (--count)
{
//do something
}
}

If the parameter was const, nothing would change for the caller of the
function, but I would have to add another local variable just to copy the
count value to it.

Jul 23 '05 #2
Rolf Magnus <ra******@t-online.de> wrote in message news:<d1*************@news.t-online.com>...
Richard Cavell wrote:
Hi,

The point of using const on a parameter to a function should be to let
your compiler know that the parameter shouldn't be modified during your
program. This allows you to keep your code safe and bug-free.

Now, it also occurs to me that a const something-or-other could be
passed as a reference (since it's guaranteed not to change) , or that
the address of the object could be passed rather than the whole thing in
the case of a large object. Is this generally done by the compiler?


No. It must be done by you. If you want to pass by reference, just pass by
reference.
Is this one of the good reasons for using const?


It is _the_ reason for using const parameters for me. I don't use const for
parameters passed by value, since those can't modify the passed value
anyway. I often do things like:

void foo(int count)
{
while (--count)
{
//do something
}
}

If the parameter was const, nothing would change for the caller of the
function, but I would have to add another local variable just to copy the
count value to it.

There might be a case when you may want to use the const for a
parameter passed by value:

void foo(const int count)
{
int test = 1;
if( count == test)
do something
}

if you accidentally mistype = for == in test (count == test) the
compiler will tell you.

Greetings,
Mihnea
Jul 23 '05 #3
Rolf Magnus wrote:
Richard Cavell wrote:

The point of using const on [a function argument]
should be to let your compiler know that
the parameter shouldn't be modified during your program.
It is a promise to the compiler that
the function won't modify the argument.
This allows you to keep your code safe and bug-free.
But it doesn't guarantee it.
Some C programmers lie to their compilers.
Now, it also occurs to me that
a const something-or-other could be passed [by const] reference
(since it's guaranteed not to change)
or that the address of the object could be passed
rather than the whole thing in the case of a large object.
The same thing may have "occurred" to Bjarne Stroustrup. :-)
Is this generally done by the compiler?
Yes.
No. It must be done by you.
If you want to pass by reference,
just pass by reference.


You are confused.
The compiler will pass by reference
if and only if the function argument is declared to be a reference.
Is this one of the good reasons for using const?


It is _the_ reason for using const parameters for me.
I don't use const for parameters passed by value,
since those can't modify the passed value anyway.
I often do things like:

void foo(int count) {
while (--count) {
//do something
}
}

If the parameter was const,
nothing would change for the caller of the function,
but I would have to add another local variable
just to copy the count value to it.


Correct if the argument also serves as a local variable.

The function declaration

void foo(const int count);

means the same thing as the function declaration

void foo(int count);

as far as the compiler is concerned
but you should *always* use the const qualifier
in the function *definition*:

void foo(const int count) {
int test = 1;
if (count == test)
// do something
}

if the argument should remain constant
in the body of the function.
Jul 23 '05 #4
E. Robert Tisdale wrote:
Now, it also occurs to me that a const something-or-other could be
passed [by const] reference (since it's guaranteed not to change) Is this generally done by the compiler?

Yes.
No. It must be done by you.
If you want to pass by reference,
just pass by reference.


You are confused.


Nope, unless I misunderstood the question. I read it as "Does the compiler
automatically pass by reference if you make the parameter const?"
The compiler will pass by reference if and only if the function argument
is declared to be a reference.


And that's exactly what I wrote above.

Jul 23 '05 #5
On 17/3/05 6:21 AM, Rolf Magnus wrote:
Nope, unless I misunderstood the question. I read it as "Does the compiler
automatically pass by reference if you make the parameter const?"


To clarify, I'm asking "Does the compiler's optimizer, when turned on,
automatically turn it into a reference if you make the parameter const?"
Jul 23 '05 #6
Richard Cavell wrote:
Rolf Magnus wrote:
Nope, unless I misunderstood the question. I read it as,
"Does the compiler automatically pass by reference
if you make the parameter const?"
To clarify, I'm asking,
"Does the compiler's optimizer, when turned on, automatically turn it


Is "it" the formal function argument or the actual function argument?
into a reference if you make the parameter const?"


As I understand both of you,
you mean both *formal* and *actual* argument when you say "parameter".

If you declare:

void foo(const int);

the compiler *must* pass an int by value when you invoke

foo(count);

If you declare

void foo(const int&);

the compiler must pass a const reference to an int when you invoke

foo(count);

If the *definition*:

void foo(const int count) {
int test = 1;
if (count == test)
// do something
}

is visible to the compiler when you invoke

foo(count);

the compiler may *inline* the function definition
whether it is declared to be inline or not.

Otherwise, the conpiler cannot "change" the declaration:

void foo(const int);

to

void foo(const int&);

and pass a const reference to an int
because the actual [external] definition
expects the compiler to pass a [const] int by value.
This is certainly what Rolf Magnus wrote/meant.


Jul 23 '05 #7
Richard Cavell wrote:

Now, it also occurs to me that a const something-or-other could be
passed as a reference (since it's guaranteed not to change) , or that
the address of the object could be passed rather than the whole thing in
the case of a large object. Is this generally done by the compiler?
Each object in C++ program has its own "address identity", meaning that
different objects must have different addresses as returned by the
built-in '&' operator (unless unions are involved). A non-reference
parameter of a function and the actual argument are two separate
objects. Each shall have its own distinct address. Programs might rely
on this fact

void foo(const large_type l, large_type* p)
{
if (&l == p)
cout << "This compiler appears to be broken!!!" << endl;
// Assuming that built-in '&' is used
}

int main()
{
large_type l;
foo(l, &l);
}

Of course, the compiler can avoid problems if it is smart enough to
detect all situations where this optimization will adversely affect the
observable behavior of the program. But I don't know of any such compiler.
Is this one of the good reasons for using const?


With a "smart" compiler - yes.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bolin | last post: by
39 posts views Thread by JKop | last post: by
6 posts views Thread by Senthilvel | last post: by
16 posts views Thread by Steven T. Hatton | last post: by
8 posts views Thread by lwawrzyniak | last post: by
16 posts views Thread by hzmonte | last post: by
4 posts views Thread by grizggg | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.