By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

const and standard types

P: n/a
What would be the rational for using const with standard types?

const int & OR const unsigned long &

Thanks.
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.