Sign in to follow this  
Basiror

C++ correctness question

Recommended Posts

Basiror    241
Hi, just a little question about correctness Lets say I have a class class dooo { const static uint32 dim = 3; float vec[3]; } is it guaranteed by the standard that the constant value will be moved out into the data segment to all the other constants? thx in advance

Share this post


Link to post
Share on other sites
Bregma    9199
Quote:
Original post by Basiroris it guaranteed by the standard that the constant value will be moved out into the data segment to all the other constants?
No. The compiler is free to emit immediate loads (or stores) instead of emitting any value to a data segment. There is not reason to store the constant in the data segment if it can be directly represented in the executable code.

--smw

Share this post


Link to post
Share on other sites
Enigma    1410
Just to reinforce, the only special guarantee made about const static integrals with constant-initializers in the C++ standard is that they can be used in integral constant expressions. Furthermore, the C++ standard makes no reference to a "data segment".

Σnigma

Share this post


Link to post
Share on other sites
Basiror    241
I could replace the const static with a enum, to get rid of the possibly additional bytes, I have this in my vector class as private member, so I could adjust it to any dimension but I want to use the vector3d class as a vertex object now that is directly passed to opengl

Share this post


Link to post
Share on other sites
Zahlman    1682
static members (regardless of cv qualifiers) are per-class, not per-object, so they shouldn't affect the sizeof() the object, if that's all you're worried about. It's not guaranteed how the member will be represented, but AFAIK, it *won't* be represented as "part of the data layout of every instance". Guys?

Share this post


Link to post
Share on other sites
Sharlin    864
Quote:
9.4.2:1
A static data member is not part of the subobjects of a class. There is only one copy of a static data member shared by all the objects of the class.


EDIT: AFAICS, though, there could be a conforming implementation where each object contains a pointer to every static data member. Then again, so could there be one that inserts a megabyte of dummy data into every object; the Standard doesn't give guarantees about object sizes except for POD types. We just have to trust that implementations don't do anything overly stupid.

[Edited by - Sharlin on December 15, 2006 4:19:47 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this