const and standard types

C

cppsks

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

const int & OR const unsigned long &

Thanks.
 
V

Victor Bazarov

cppsks said:
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
 
A

Andrey Tarasevich

cppsks said:
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; // 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...
 
S

Siemel Naran

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

cppsks

Siemel Naran said:
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
 
S

Siemel Naran

cppsks said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top