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

L

Lighter

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.
 
M

Michael Taylor

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

I don't think VC++ 2005, broke those rules.
In each case you are using a struct containing a bit field, not a bit-field.
 

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,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top