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.