const vs. static const inside class

J

John Goche

Hello,

I would like to know whethere there is a difference between
a const variable and a static const variable inside a class.
After all, if a variable is const in a class, the compiler can
always optimize and turn it into a static const variable to
save runtim memory/stack space since being const it
cannot be changed. (?)

Thanks,

JG
 
M

Mark P

John said:
Hello,

I would like to know whethere there is a difference between
a const variable and a static const variable inside a class.
After all, if a variable is const in a class, the compiler can
always optimize and turn it into a static const variable to
save runtim memory/stack space since being const it
cannot be changed. (?)

Thanks,

JG

A non-static constant member variable may have a different constant
value for different instances of the class.
 
V

Victor Bazarov

John said:
I would like to know whethere there is a difference between
a const variable and a static const variable inside a class.
After all, if a variable is const in a class, the compiler can
always optimize and turn it into a static const variable to
save runtim memory/stack space since being const it
cannot be changed. (?)

I am not sure under what circumstances the non-static const data
member can be optimized away. If the object is non-const to begin
with, something can always cast away the const-ness and change the
data member without much of a concern, really. So, the data member
has to exist per instance. A static const data member exists per
class, so it definitely can be placed in a const storage and using
a const cast to remove const-ness on it would result in undefined
behaviour. That's why it can actually be optimized by the compiler
and be used as a literal in many cases. That's the difference,
AFA I'm concerned.

V
 
N

Noah Roberts

Victor said:
I am not sure under what circumstances the non-static const data
member can be optimized away. If the object is non-const to begin
with, something can always cast away the const-ness and change the
data member without much of a concern, really.

If the data member is const then casting the object itself to/from
const has no affect on the undefined nature of changing that member.
So I don't think it needs to be a different object per instance for
that reason.

However, since you can initialize const data members with data passed
into the constructor those values can be different per instance to
begin with and can be based on runtime calculated values. For that
reason most of the time the data member needs to be one per instance
and can't be optimized away. I doubt any compiler author would spend
much time looking for the few times when it could be.

A code sample to illustrate for the OP:

struct X
{
const int x;
X() : x(0) {}
X(int y) : x(y) {}
};

int main(void)
{
X x1;
X x2(5);
X x3(9);

std::cout << x1.x << "\n";
std::cout << x2.x << "\n";
std::cout << x3.x << "\n";

int y;
std::cin >> y;

X x4(y);

std::cout << x4.x << "\n";
}
 
E

Evan

Victor said:
I am not sure under what circumstances the non-static const data
member can be optimized away. If the object is non-const to begin
with, something can always cast away the const-ness and change the
data member without much of a concern, really.

My impression was that if you did this to an object that was declared
const the result was undefined behavior. Am I mistaken on this point?

My understanding was if you have a non-const object that gets turned
into a const object at some point, you can cast away the constness, but
if the object was declared const then you can't be guaranteed that it
will work as you wish.

(Though in the context of this discussion, as other posters have said
there is a difference between const and static const because a const
member just has to be const within any given object, but different
objects may have different values for that variable.)

Evan
 
V

Victor Bazarov

Evan said:
My impression was that if you did this to an object that was declared
const the result was undefined behavior. Am I mistaken on this point?

No, but if you do it in a member function that is not declared 'const',
what could be wrong? Don't do it in a c-tor or d-tor, of course.
My understanding was if you have a non-const object that gets turned
into a const object at some point, you can cast away the constness,
but if the object was declared const then you can't be guaranteed
that it will work as you wish.

Any data member of a non-const object is non-const to begin with (the
memory is allocated in a mutable area of the program full storage).
The 'const' qualifier for a member is a precaution, not a lock.
(Though in the context of this discussion, as other posters have said
there is a difference between const and static const because a const
member just has to be const within any given object, but different
objects may have different values for that variable.)

Yes. You initialise any non-static data member in the constructor
initialiser list. Since it is possible to give different values to
const non-static members, that's probably why they are non-static.

V
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top