473,993 Members | 50,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2390
* 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
2870
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 modify it as requirements change. More generally, in fact, they want a general package of functions that will anticipate *all* requirements. They must be called as macros. I can appreciate that a simple sequence of operations could be
5
2223
by: | last post by:
Hi,all U can download dotNetHookLibrary in http://dotnethook.sourceforge.net . for instance, I have the fellowing code : //------------------------------------------------------------------------ using System; namespace hook { class Class1
10
2074
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 Inetpub/wwwroot/mywebsite/ Thanks.
22
15378
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 reproduce the behavior described below? For this example, I am using Excel 2002 and VS .NET 2002 and VB 6. MSFT KB article 304661 gives a trivial example of early and late binding to Excel from VB .NET. Note that there is a variable naming...
24
2685
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="zh-tw" /> knowing all the time as...
0
11928
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11495
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10998
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8554
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3843
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.