Is a const parameter generally optimized to a reference?

R

Richard Cavell

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?
 
R

Rolf Magnus

Richard said:
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.
 
M

Mihnea

Rolf Magnus said:
No. It must be done by you. If you want to pass by reference, just pass by
reference.


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
 
E

E. Robert Tisdale

Rolf said:
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.

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. :)
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.
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.
 
R

Rolf Magnus

E. Robert Tisdale said:
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.
 
R

Richard Cavell

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?"
 
E

E. Robert Tisdale

Richard said:
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.
 
A

Andrey Tarasevich

Richard said:
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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top