473,406 Members | 2,894 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,406 software developers and data experts.

const &

I understand that compiler may optimize better, if as parameters to my
functions i pass:

const Object &o

instead of

Object &o

All the implicit type conversion and all sounds good (Effective C++ rules).

But, does it make any difference if i do simillar thing for primitive types:

function(int x) vs function(const int x)

and i know that x will not be assigned to in the body of the function.
Is there any advantage at all in putting the const qualifier for primitives?

Thanks,
Martin
Jul 19 '05 #1
4 3895
Marcin Vorbrodt wrote:
I understand that compiler may optimize better, if as parameters to my
functions i pass:

const Object &o

instead of

Object &o
I don't know about that. But it does make the function more flexible
since you can pass temporaries and constant objects. There's also no
good reason to /not/ use const here, if you don't intend to change the
object.

All the implicit type conversion and all sounds good (Effective C++ rules).

But, does it make any difference if i do simillar thing for primitive types:

function(int x) vs function(const int x)

and i know that x will not be assigned to in the body of the function.
Is there any advantage at all in putting the const qualifier for primitives?


I don't think so.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
On Sun, 07 Sep 2003 15:16:31 -0400, Marcin Vorbrodt wrote:
I understand that compiler may optimize better, if as parameters to my
functions i pass:

const Object &o

instead of

Object &o


No, you (might) get better performance if you use

const Object& o

instead of

Object o

Josh
Jul 19 '05 #3
"Oliver S." wrote:
In the unlikely case using the const qualifier does help performance
don't expect a big boost.


The called code is allowed to cast to a non-const reference if the
underlying object has only logical (as opposed to physical) constness.
So the compiler can assume that the object's non-mutable elements are
not changed only if the object has physical constness - which is very
unusual.


The true rule is that you can cast away const if the original object
isn't const. Otherwise you get undefined behavior. A common example is:

int main()
{
const int i = 3;
int& ir = const_cast<int&)(i);
ir = 4;
printf("%d\n", i);
return 0;
}

With some compilers this prints 3, with others 4. Both answers are
acceptable, because the behavior of the program is undefined.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #4
"Oliver S." wrote:
The true rule is that you can cast away const if the original
object isn't const.


That's only one of the rules which describe objects that have
logical constness.


Maybe. The obvious implication is that "logical constness" isn't the
test for casting away const.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
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...
11
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
3
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const...
6
by: p|OtrEk | last post by:
What is practic difference between this two declarations? If i want call my func with func("blah") i could write: 1) func(std::string const& arg1) 2) func(const std::string& arg1) Whats better to...
0
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it...
2
by: TonyM | last post by:
Q: Is there a good way to overcome this apparent bug without modifying the mfc code? ___________________________________ Info: Although it is NOT a good idea, I seemed to have perhaps located a...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
1
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
2
by: dolphin | last post by:
Why should we use fun(const Class &B) instead of fun(Class &B)?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.