Article View: comp.std.c++
Article #3739Bit-field specification incomplete?
From: scott douglass
Date: Tue, 19 Mar 1996 17:47
Date: Tue, 19 Mar 1996 17:47
74 lines
3903 bytes
3903 bytes
Hello,
I was surprised to find that section 9.6, Bit-fields, does not prohibit
'struct T { int i:-1; };' or 'struct T { int i:0; };' which are constraint
violations in the C standard. It also does not explicitly state that the
member must act as an integer of the specified numbers of bits. As it
stands I think a compiler can ignore the width specification entirely
except that it must prevent the address of the field from being taken and
prevent references to the field. [I'm assuming the phrase "shall not"
means a program that tries to do so is ill-formed.]
Should this be fixed?
[The rest of this seems pretty archane, even to me but it seems like the
answers should be in the standard.]
Since the draft says nothing specifically about the type of a bit-field
member, I assume that it is the type of the member ignoring the bit-field
specifier. [Although in section 7.2, Enumeration declarations, it says
"The underlying type of an enumeration shall not be a bitfield." implying
that there is such a thing as a bit-field type.] So given,
enum E { k0, k1, k2 };
struct T { bool b:2; char c:2; short s:2; int i:2; long l:2; E e:2; };
void g(bool);
void g(char); void g(unsigned char); void g(signed char);
void g(short); void g(unsigned short);
void g(int); void g(unsigned int);
void g(long); void g(unsigned long);
void g(E);
void f(const T* t)
{
g(t->b); // calls g(bool);
g(t->c); // calls g(char);
g(t->s); // calls g(short);
g(t->i); // calls g(int);
g(t->l); // calls g(long);
g(t->e); // calls g(E);
}
[And so on for 'unsigned char', 'signed char', 'wchar_t', 'unsigned
short', 'short', 'unsigned int' and 'unsigned long'.]
Is this correct? Or does the type of a bit-field depend on the
implementation-defined signedness of bit-fields? If so, then on an
unsigned plain bit-field machine I would imagine the type of 't->i' above
would be 'unsigned int' but what is the type of 't->c' -- 'char' or
'unsigned char'?
[Are there 'unsigned wchar_t' or 'signed wchar_t'? I don't think so, but,
section 3.9.1, Fundamental Types, does not explicitly prohibit them as it
does for 'bool' and does not explicitly allow them as it does for 'char'.]
Finally, the promotion of a bit-field is specified in 4.5, Integral
promotions. It specifies that all non-enum bit-fields promote to 'int' or
'unsigned int'. This seems wrong for 'long' and 'unsigned long'
bit-fields. Why should they "promote" to 'int' or 'unsigned int'? I
would expect a bit-field of type 'long' to have type 'long' or 'unsigned
long' even if the width is short enough to fit in an 'int'. After all, a
bit-field of enum type where the enum range is too big for 'unsigned int'
promotes to 'long' or 'unsigned long' even if the width is short enough to
fit in an 'int'.
Thanks,
--scott
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<sdouglas-1903961604090001@193.131.176.202>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: sdouglas@armltd.co.uk (scott douglass) Newsgroups: comp.std.c++ Subject: Bit-field specification incomplete? Date: 19 Mar 1996 09:47:31 PST Organization: Apple Computer, Inc. Approved: austern@isolde.mti.sgi.com Message-ID: <sdouglas-1903961604090001@193.131.176.202> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: Tue, 19 Mar 1996 16:04:09 +0000 X-Newsreader: Yet Another NewsWatcher 2.0.2 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMU7zSUy4NqrwXLNJAQFU8wIAg7hj7IOGQ6NNYDZ3fCsAO/j4uktLRG/3 OjwPEIAHtXTw2SZhEnmzb0+Kx4hJz9BbLfA/8R8Gw3eknLLEvo2z0g== =F6O3 Originator: austern@isolde.mti.sgi.com