473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why shall a non-const reference not be bound to a bit-field?

On page 163 of In the C++ standard document(9.6 Bit-fields), I find
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
committee made such restrictions. I cannot 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.

Jul 6 '06 #1
1 2345
* Lighter:
On page 163 of In the C++ standard document(9.6 Bit-fields), I find
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
committee made such restrictions. I cannot find enough motivation for
the C++ standard committee to do like this. I think VS 2005 did right
to break these rules.
The above code does not demonstrate any rule breaking.

A static bit-field would be like

struct Foo { static int a : 1; };

And so on -- please post your validation results with the code changed
to use actual bitfields, not structs.

If you know the whys, please tell me. Thanks in advance.
For the why's of the standard, please ask in [comp.std.c++].

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: David Powell | last post by:
Because my work area won't have an Access programmer next year, I've been asked to rebuild their coded application as a set of modular tools. The idea is that a non-programmer will be able to...
5
by: | last post by:
Hi,all U can download dotNetHookLibrary in http://dotnethook.sourceforge.net . for instance, I have the fellowing code :...
10
by: vwd2005eeb | last post by:
Visual Web Developer 2005 Express Edition Beta I did Build | Build Web site, where are the DLLs? I was expecting to see maybe one dll for each Web page in a directory (maybe still "bin") under...
22
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody...
24
by: Dan Jacobson | last post by:
I shall jump on the XHTML bandwagon. I run my perfectly good html4/strict pages thru $ tidy -asxhtml -utf8 #to get: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.