473,883 Members | 2,233 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1453
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
5071
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 stupid to understand and make them work. E.g. I have read that boost's smart pointers are able to do this convertion, but the following code doesn't compile (VC++6.0):
3
1981
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, my compiler doesn't have temporary const objects. Are there any reasons to have a const reference parameter ? Thanks in advance for your help
39
3111
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
4876
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 a parameter passed by reference or const reference. But i do not think so. I looked up in the TCPPPL but nothing of that sort is explicitly mentioned(or have i missed it ??? ).
16
2500
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 could set that qualifier in the function call. Can that be done? #include <iostream> using std::ostream; using std::cout;
10
1550
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 at least flag this as a warning as they would when say trying to return a const & to a local? In Section #2, the const B& Bref is initialized and bound to the temporary returned from GetSettings(). That is the temporary B exists until Bref goes...
8
1448
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; /* ok */ int **c; /* pointer to (pointer to an integer) */ const int **d; /* pointer to (pointer to a constant integer) */ d = c; /* not ok (incompatible pointer type) */
16
3173
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 parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
4
6705
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 = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
0
9945
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9798
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10766
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.