• Advertisement
Sign in to follow this  

Why did the C++ standard committee make such restrictions on bit-fields?

This topic is 4248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

On page 163 of the C++ standard document(9.6 Bit-fields), I found three rules on bit-fields: Rule 1, "A bit-field shall not be a static member." Rule 2, "A non-const reference shall not be bount to a bit-field" Rule 3, "Note: if the initializer for a reference of type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized to hold the value of the bit-field; the reference is not bound to the bit-field directly." Visual Studio 2005, however, can correctly compile and run the following code fragment: typedef int BIT; struct BITSET { BIT a : 1; BIT b : 1; BIT c : 1; BIT d : 5; }; class Test { public: static BITSET m; // Violation of the Rule 1 }; BITSET Test::m = {0}; int main() { BITSET a = {1, 0, 1}; BITSET& b = a; // Violation of the Rule 2 const BITSET& c = a; a.a = 0; // After this statement, c.a is also set to 0. Violation of the rule 3 } Maybe someone will say: "They are just that Microsoft doesn't abide by the C++ standard", but what I want know is why the C++ standard make such restrictions. I'm not able to find enough motivation for the C++ standard committee to do like this. I think VS 2005 did right to break these rules. If you know the whys, please tell me. Thanks in advance. [Edited by - cqulyx on July 6, 2006 5:39:46 PM]

Share this post


Link to post
Share on other sites
Advertisement
BITSET is not a bit-field. BITSET::a, BITSET::b, BITSET::c and BITSET::d are bit fields that are members of the BITSET structure.

Share this post


Link to post
Share on other sites
You read the requirements incorrectly. BITSET is a struct, not a bitfield. It is BITSET::a through BITSET::d which are bitfields and thus cannot be static or bound to non-const references.

The primary reason is that on most architectures, bits are not individually addressable.

Share this post


Link to post
Share on other sites
Dude, wiping out the question after you've gotten an answer isn't cool.

Share this post


Link to post
Share on other sites
to SiCrane and uray:

I deleted the post just because I felt ashamed for having such a stupid question. I didn't think of you would feel not cool. Now I repost it again, and say sorry to you two.

Share this post


Link to post
Share on other sites
Quote:
Original post by cqulyx
to SiCrane and uray:

I deleted the post just because I felt ashamed for having such a stupid question. I didn't think of you would feel not cool. Now I repost it again, and say sorry to you two.

Never feel ashamed for asking a question. Feel ashamed if you are too arrogant to ask questions. Nobody knows everything (except me, but I don't count. Hail Eris! You shall partake of no hot dog buns!)

Share this post


Link to post
Share on other sites
I'm very sorry that I can not understand the meaning of "Hail Eris! All hail Discordia!", I googled it but get nothing.

Who can teach me what the words actually means? Thanks in advance.

Share this post


Link to post
Share on other sites
Eris and Discordia are names of deities associated with chaos, and in recent times with chaos magick.

Chaotes are known for their irreverent sense of humour and tend to veer wildly between absolute belief and joking hoax. Good people all in all [smile].

Well, you did ask.

Hail $godform!

Share this post


Link to post
Share on other sites
Quote:
Original post by cqulyx
I'm very sorry that I can not understand the meaning of "Hail Eris! All hail Discordia!", I googled it but get nothing.

Who can teach me what the words actually means? Thanks in advance.


Have a little read of the Principia Discordia and you may begin to come to the realisation that 2 rights can never make a left, as a left can only be accomplished after a writhing.

Share this post


Link to post
Share on other sites
It seems too sophisticated for me to realize.

I have no sense of Philosophy......

Share this post


Link to post
Share on other sites
nobody really answered why bit-fields have such restrictions.

Share this post


Link to post
Share on other sites
Bit fields can only be primitives because, well, how the hell is the compiler supposed to know how to compress your class into X bits?

Fruny already answered your question exactly. A static needs to be aligned. A reference is really a pointer; it needs the full size of the architecture's pointer, or won't be able to point everywhere. And it also needs to be aligned.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement