Question about constant member functions

S

Sunny

Hi,
What are the compiler optimizations that are applicable if a member
function is declared const ? What kind of better code is generated for
aconst function in terms of execution speed.
Thanks
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi,
What are the compiler optimizations that are applicable if a member
function is declared const ? What kind of better code is generated for
aconst function in terms of execution speed.

Non that I'm aware of to my knowledge it's more of a tool for the
developer. When I can a const member of an object there are certain
assumptions I can make, such that the state of the object does not
change [*] which can have some advantages, especially when trying to
verify that a program works correctly. Having said that it might be
possible that there are some optimizations that can be made also.

[*] Once can make modifications even in a const member using
const_cast, but it's not a good practice.
 
G

Gavin Deane

[*] Once can make modifications even in a const member using
const_cast, but it's not a good practice.

Not if the object was declared const one can't. Any attempt to modify
a const object leads to undefined behaviour.

Perhaps that's what you meant, but "not a good practice" didn't seem a
strong enough deterrant to me.

Gavin Deane
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

[*] Once can make modifications even in a const member using
const_cast, but it's not a good practice.

Not if the object was declared const one can't. Any attempt to modify
a const object leads to undefined behaviour.

Perhaps that's what you meant, but "not a good practice" didn't seem a
strong enough deterrant to me.

I meant something like this:

sruct Foo {
int i;
void bar() const;
}

void Foo::bar() {
Foo* fp = const_cast<Foo*>(this);
fp->i = 3;
}

int main() {
Foo f;
f.i = 3;
f.bar();
}

Now, given that bar is declared const it should normally not be able
to change the state of f, but using const_cast it's possible. I
believe this to be legal, of course, had I declared f const as well
then it would be UB.
 
G

Gavin Deane

[*] Once can make modifications even in a const member using
const_cast, but it's not a good practice.
Not if the object was declared const one can't. Any attempt to modify
a const object leads to undefined behaviour.
Perhaps that's what you meant, but "not a good practice" didn't seem a
strong enough deterrant to me.

I meant something like this:

sruct Foo {
int i;
void bar() const;

}

void Foo::bar() {
Foo* fp = const_cast<Foo*>(this);
fp->i = 3;

}

int main() {
Foo f;
f.i = 3;
f.bar();

}

Now, given that bar is declared const it should normally not be able
to change the state of f, but using const_cast it's possible. I
believe this to be legal, of course, had I declared f const as well
then it would be UB.

That's my understanding too.

Gavin Deane
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top