473,406 Members | 2,259 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 and standard types

What would be the rational for using const with standard types?

const int & OR const unsigned long &

Thanks.
Jul 22 '05 #1
6 1477
cppsks wrote:
What would be the rational for using const with standard types?

const int & OR const unsigned long &


Often a const reference to one of built-in types happens to be used
in template code, like

template <class T> void foo(T const& blah);
...
foo(42);

Nothing bad about it, AFAIK. I once did some testing and figured
that passing 'double' by value was just slightly slower than passing
it by a ref or by a pointer, but only in a debug version (or something
like that).

My rule is, "when in doubt, test". My other rule is, "for built-in
types, pass by value".

Victor
Jul 22 '05 #2
cppsks wrote:
What would be the rational for using const with standard types?

const int & OR const unsigned long &
...


Hmm... What kind of answer do you expect besides the obvious "to declare
objects of standard types as constants"? The implications of declaring
an object of standard type as constant are numerous.

Constant objects of integral type can be used in integral constant
expressions, while non-constant objects can't:

const int a = 5;
int b = 5;

char aa[a]; // OK
char bb[b]; // ERROR

Const-qualified references to standard types can be bound to rvalue objects:

const int& cr = 5; // OK
int& r = 5; // ERROR

And so on...

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #3
cppsks wrote:
What would be the rational for using const with standard types?
Just the same as for using it with your own types.
const int & OR const unsigned long &


Did you make references here for a reason?

Jul 22 '05 #4
"cppsks" <sk*****@hotmail.com> wrote in message
news:cg**********@news.lsil.com...
What would be the rational for using const with standard types?

const int & OR const unsigned long &


It can happen as part of implicit tempalte instantiation.

template <class T>
void print(const T&);

For an explicit function, I'd probably pass by value as in the release build
it would probably be faster because the compiler could optimize without
having to worry about aliasing. See Victor's post too.

If you have a function and it receives a variable by value, but in the
function definition you declare the value const, that's another story. Is
this what you're asking about?

void f(int);
void f(const int i) {
...
}
Jul 22 '05 #5

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:im*********************@bgtnsc04-news.ops.worldnet.att.net...
"cppsks" <sk*****@hotmail.com> wrote in message
news:cg**********@news.lsil.com... If you have a function and it receives a variable by value, but in the
function definition you declare the value const, that's another story. Is
this what you're asking about?

void f(int);
void f(const int i) {
...
}


Thanks for the response to all. Just a quick question about the above
declartion and definition.
What does the above convention mean?

Thanks,

skscpp
Jul 22 '05 #6
"cppsks" <sk*****@hotmail.com> wrote in message
news:cg**********@news.lsil.com...
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message

void f(int);
void f(const int i) {
...
}


Thanks for the response to all. Just a quick question about the above
declartion and definition.
What does the above convention mean?


Usually we declare the function variables without top-level const, but may
define them with const to prevent us from accidentally changing the
variable, and making it clear to the person reading the code that in the
body of function f, variable 'i' will not change its value. Doubtless, this
practice is useless for short functions which are simple to understand
anyway, but does pay off in larger and/or complicated functions.
Jul 22 '05 #7

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...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
7
by: BigMan | last post by:
Which is the preferred way to create a const container in general: 1. container_type< value_type > const; or 2. container_type< value_type const >?
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
9
by: t | last post by:
Can you use a plain iterator to vector<const intto write to it? It doesn't seem like it should be possible, but Visual C++ 2005 Express lets me: vector<const intvc; vc.push_back(5);...
10
by: parag_paul | last post by:
I have a non member function , which has a prototype const print() const{ What could be the purpose of the same?
4
by: lovecreatesbea... | last post by:
Gcc only gives out a warning: `assignment discards qualifiers from pointer target type' against code such as following: $ type a.c int main(void) { const char *pc; char *p = pc;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
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
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,...
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
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
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.