-pedantic-errors, ? : and ptrs to base classes

Martin von Loewis martin@mira.isdn.cs.tu-berlin.de
Sun Sep 6 05:27:00 GMT 1998


> struct B {};
> struct D1 : public B {};
> struct D2 : public B {};
> 
> B *
> f( int i, D1 * pD1, D2 * pD2)
> {
> 	return i ? pD1 : pD2;
> };

I believe g++ is right in rejecting this under pedantic mode.

5.16, [expr.cond]/6 defines the type of this expression:

>> After those conversions, one of the following shall hold: ...
>> - The second and third operands have pointer type, or one has
>> pointer type and the other is a null pointer constant; pointer
>> conversions (4.10) and qualification conversions (4.4) are
>> performed to bring them to their composite pointer type (5.9). The
>> result is of the composite pointer type.

Both operands have pointer type, and neither is a null pointer
constant. Now lets see what the composite type of these expressions
is, according to 5.9, [expr.rel]/2:

>> If one operand is a null pointer constant, the composite pointer
>> type is the type of the other operand. Otherwise, if one of the
>> operands has type "pointer to cv1 void", then the other has type
>> "pointer to cv2 T" and the composite pointer type is "pointer to
>> cv12 void", where cv12 is the union of cv1 and cv2. Otherwise, the
>> composite pointer type is a pointer type similar (4.4) to the type
>> of one of the operands, with a cv­qualification signature (4.4)
>> that is the union of the cv­qualification signatures of the operand
>> types.

Since pD1 and pD2 are not similar, the expression is not well-formed.

Regards,
Martin



More information about the Gcc-bugs mailing list