C++ Conditional Operator

Hi,

I'll implement C++ semantics for ?: over the next few days. Because the
operator has awful semantics, I've decided to use a test-first approach
to it. I'd appreciate a review of my test case before I go and implement
something incorrect.

Sebastian

conditional-expr.cpp (3.2 KB)

For the bit-fields, a test like "(i1 ? flds.b1 : flds.b2) = 0;" would be nice.

For the p2 tests, a test that should fail like "1 ? (void)0 : 0;" would be nice.

For the p3 tests, one or two tests with private bases might be useful,
to make sure we're actually finding the right conversion.

I'm not that familiar with the details of the C++ rules, but none of
the tests looks obviously wrong.

-Eli

Hi,

I'll implement C++ semantics for ?: over the next few days. Because the
operator has awful semantics,

Yes. Yes it does. Thanks for tackling this!

I've decided to use a test-first approach
to it. I'd appreciate a review of my test case before I go and implement
something incorrect.

Your tests look good to me, and EDG agrees in both relaxed and strict mode.

  - Doug

Eli Friedman wrote:

  

I'll implement C++ semantics for ?: over the next few days. Because the
operator has awful semantics, I've decided to use a test-first approach
to it. I'd appreciate a review of my test case before I go and implement
something incorrect.
    
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;

?

For the p2 tests, a test that should fail like "1 ? (void)0 : 0;" would be nice.
  
Mixing void and non-void; good catch.

For the p3 tests, one or two tests with private bases might be useful,
to make sure we're actually finding the right conversion.

True. Also, ambiguous bases.

Thanks,
Sebastian

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