| View previous topic :: View next topic |
| Author |
Message |
cppsks Guest
|
Posted: Wed Aug 25, 2004 7:29 pm Post subject: const and standard types |
|
|
What would be the rational for using const with standard types?
const int & OR const unsigned long &
Thanks.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Aug 25, 2004 8:01 pm Post subject: Re: const and standard types |
|
|
cppsks wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Wed Aug 25, 2004 11:00 pm Post subject: Re: const and standard types |
|
|
cppsks wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Aug 26, 2004 12:50 am Post subject: Re: const and standard types |
|
|
cppsks wrote:
| Quote: | What would be the rational for using const with standard types?
|
Just the same as for using it with your own types.
| Quote: | const int & OR const unsigned long &
|
Did you make references here for a reason?
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Thu Aug 26, 2004 2:59 am Post subject: Re: const and standard types |
|
|
"cppsks" <sksjava (AT) hotmail (DOT) com> wrote
| Quote: | 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) {
...
}
|
|
| Back to top |
|
 |
cppsks Guest
|
Posted: Sat Aug 28, 2004 4:16 am Post subject: Re: const and standard types |
|
|
"Siemel Naran" <SiemelNaran (AT) REMOVE (DOT) att.net> wrote
| Quote: | "cppsks" <sksjava (AT) hotmail (DOT) com> wrote in message
news:cgipa9$203$1 (AT) news (DOT) 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
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Sat Aug 28, 2004 4:54 am Post subject: Re: const and standard types |
|
|
"cppsks" <sksjava (AT) hotmail (DOT) com> wrote
| Quote: | "Siemel Naran" <SiemelNaran (AT) REMOVE (DOT) 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.
|
|
| Back to top |
|
 |
|