This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: typeof and bitfields


On 2005-01-21, Nathan Sidwell <nathan@codesourcery.com> wrote:
> 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_})

(with a ';' inserted before the closing '}'.)

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

It seems to.

Neither version will work for C++ classes which define ordering but not
arithmetic operations, but then in C++ it really makes more sense to use
std::max() anyway and avoid GCC specific extensions.

> 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.

Actually, in this case it makes no difference, since ?: performs integral
promotions on its second and third arguments, so the type of the
statement expression is already int with the original version of the max
macro in all the cases that adding 0 affects.

Cheers,
    Olly


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]