Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const and standard types

Reply
Thread Tools

const and standard types

 
 
cppsks
Guest
Posts: n/a
 
      08-25-2004
What would be the rational for using const with standard types?

const int & OR const unsigned long &

Thanks.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-25-2004
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
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      08-25-2004
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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-26-2004
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?

 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      08-26-2004
"cppsks" <> wrote in message
news:cgipa9$203$...

> 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) {
...
}


 
Reply With Quote
 
cppsks
Guest
Posts: n/a
 
      08-28-2004

"Siemel Naran" <> wrote in message
news:imcXc.516998$...
> "cppsks" <> wrote in message
> news:cgipa9$203$...


> 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


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      08-28-2004
"cppsks" <> wrote in message
news:cgp0ue$d3c$...
> "Siemel Naran" <> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments