[cfe-dev] C++ Conditional Operator

Eli Friedman eli.friedman at gmail.com
Mon Apr 13 09:23:10 PDT 2009


On Mon, Apr 13, 2009 at 9:14 AM, Sebastian Redl
<sebastian.redl at getdesigned.at> wrote:
>> For the bit-fields, a test like "(i1 ? flds.b1 : flds.b2) = 0;" would be nice.
>
> Doh! Of course I can test lvalue-ness of bitfields by assigning.
>
> Hmm ... is it specified anywhere what this snippet does?
>
> struct { unsigned a : 3, b: 5; } fields;
> (coinflip() ? fields.a : fields.b) = 10;
>
> Does some sort of size unification happen between the two bitfields, or
> are the semantics exactly the same as
>
> if(coinflip()) fields.a = 10; else fields.b = 10;
>
> ?

g++ does the latter; here's a testcase that shows that:
int f(int X){struct{unsigned a:3,b:5;}fields;return(X?fields.a:fields.b)=10;}

IMO, it's the only approach that really makes sense...

-Eli



More information about the cfe-dev mailing list