473,379 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 1423
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
3
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But,...
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
6
by: Senthilvel | last post by:
Hi folks, My friend tells that the following function declaration is illegal. void Foo(const string& strData = "Default"); My friend argues that it is not legal to provide a default value for...
16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
10
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers...
8
by: lwawrzyniak | last post by:
I'm having some fun with const and multiple indirection. Consider the following: int *a; /* pointer to an integer */ const int *b; /* pointer to a constant integer */ b = a; ...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.