typeof and bitfields

Nathan Sidwell nathan@codesourcery.com
Fri Jan 21 10:52:00 GMT 2005


Tom Tromey wrote:
>>>>>>"Mark" == Mark Mitchell <mark@codesourcery.com> writes:
> 
> 
> Mark> I understand the utility of typeof, but lots of people get by without
> Mark> using typeof at all.  I'm not going to believe that removing the
> Mark> ability to apply typeof to a bitfield is going to cause very many
> Mark> problems for very many people.  How many bug reports have we gotten so
> Mark> far?
> 
> I don't know whether we've gotten bug reports, but I did see some
> discussion of this problem a while back.  Apparently the linux kernel
> used this.
> 
> If we do make this change, IMO we should also patch the manual to say
> that the max() macro in the example won't work for bitfields.

Note that it can be fixed by removing the lvalueness from the bitfield.
For all instances where max is applicable, one could write it as

#define max(x, y) ({typeof (x+0) x_ = x; typeof(y+0) y_ = y; x_ > y_ ? x_ : y_})

Perhaps even unary+ would suffice, I'm not sure.

This will make the type of max(x,y) an int, when x and y are shorts or chars,
but I don't think the user could tell -- unless they themselves wrapped the max
in a sizeof or typeof.

nathan
-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk



More information about the Gcc mailing list