How to use _Generic with bit-fields
Martin Sebor
msebor@gmail.com
Tue Feb 23 00:24:00 GMT 2016
On 02/21/2016 06:44 PM, Wink Saville wrote:
> I've tried you hack, but it doesn't work with "long bit fields". Also,
> I've run into another problem. Instead of using unsigned char for
> the bit field I changed it to a long unsigned int:33 and now I can't
> print it without a warning.
That's unfortunate. As I suggested before, it would be useful to
add your test cases to bug 65471 to help bring them to the committee's
attention. The bug submitter especially seems to be interested in
making the feature more useful and while he may not be reading this
list he is likely to notice the bug update.
The purpose of the generic selection in C is to provide a limited
form of function overloading. Calling functions with bitfield
arguments is obviously useful regardless of the bit-field width
so it should "just work" with _Generic. It does with other
compilers (Clang and the EDG front end) and there's no good reason
for the C standard not to support it. (The problem is simply that
wording that specifies the feature was poorly chosen and GCC seems
to follow it a bit too strictly, and to the detriment of usability.)
I'm sorry but I can't think of a way to make this work the way you're
trying to. Again, I encourage you to add it to the existing bug or
open a new one.
Martin
PS From some of the followups downthread it looks to me as though
the problem might be thought to be the bit-field width in your test
case exceeding that of int. The following example shows that the
same problem error is reported even with a smaller bit-field.
$ cat z.c && /home/msebor/build/gcc-trunk-svn/gcc/xgcc
-B/home/msebor/build/gcc-trunk-svn/gcc -Wall -Wextra -Wpedantic -xc z.c
struct S { unsigned i: 31; } s;
int i = _Generic (s.i, unsigned: 1);
z.c:2:19: error: â_Genericâ selector of type âunsigned int:31â is not
compatible with any association
int i = _Generic (s.i, unsigned: 1);
^
More information about the Gcc
mailing list