This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: How to use _Generic with bit-fields
- From: Martin Sebor <msebor at gmail dot com>
- To: Wink Saville <wink at saville dot com>
- Cc: GCC Development <gcc at gcc dot gnu dot org>
- Date: Mon, 22 Feb 2016 17:24:44 -0700
- Subject: Re: How to use _Generic with bit-fields
- Authentication-results: sourceware.org; auth=none
- References: <CAKk8iso7KLotY1WGwaO8FBvp27nFORAWdShqVGth=0Hrc+WVbQ at mail dot gmail dot com> <56C78A87 dot 4090601 at gmail dot com> <CAKk8isoF4gT_z9sscYmnetMZOCkaZUO0Gr9_sj7WQB86PEQxZg at mail dot gmail dot com>
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);
^