RE: [PATCH] linux/const.h: Explain how __is_constexpr() works

From: David Laight
Date: Wed Feb 02 2022 - 17:20:35 EST


From: Miguel Ojeda <miguel.ojeda.sandonis@xxxxxxxxx>
> Sent: 02 February 2022 20:14
> To: David Laight
>
> On Wed, Feb 2, 2022 at 5:19 PM David Laight <David.Laight@xxxxxxxxxx> wrote:
> >
> > - The conditional operator (?:) requires that both expressions have the
> > the same type (after numeric promotions).
>
> I think you are referring to the arithmetic types case, but that one
> does not apply here.

The type of the result depends on the type of the 2nd and 3rd arguments.
Not on the value of the first one.

> > - If the expressions have distinct non-NULL pointer types then they are both
> > cast to (void *) and the result has type 'void *'.
>
> GCC and Clang give `void *` for this, but something like `float *` and
> `int *` do not fit the constrains of the operator.

Compiling:

#define k(x) (sizeof(*(1 ? (float *)8 : (int *)8)))

int f1(void){ return k(6); }

gives:

f1:
movl $1, %eax
ret

(see https://godbolt.org/z/e1MvYYxGz)

Change to #define k(x) (sizeof(*(1 ? (void *)(x) : (int *)0)))
and you only get 4 when x is a compile-time constant 0.

It has nothing to with the condition, the compiler is trying to 'sort out'
a suitable return type.

I suspect the mismatched pointer types might even be a gcc extension.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)