Re: typecheck code

From: Geert Uytterhoeven
Date: Mon Jan 31 2011 - 12:23:04 EST


On Mon, Jan 31, 2011 at 18:03, Sri Ram Vemulpali
<sri.ram.gmu06@xxxxxxxxx> wrote:
> Hi all,
>
> /*
> Â* Check at compile time that something is of a particular type.
> Â* Always evaluates to 1 so you may use it easily in comparisons.
> Â*/
> Â#define typecheck(type,x) \
> Â({ Â Â Âtype __dummy; \
> Â Â Â Âtypeof(x) __dummy2; \
> Â Â Â Â(void)(&__dummy == &__dummy2); \
> Â Â Â Â1; \
> Â})
>
> #define typecheck_fn(type,function) \
> ({ Â Â Âtypeof(type) __tmp = function; \
> Â Â Â (void)__tmp; \
> })
>
> Can anyone help me, explain the above code typecheck. How does
> (void)(&__dummy == &__dummy2) evaluates to 1

It does not rely on the equation evaluating to 1. That result is
expicitly unused,
witness the cast to void.
It does rely on gcc complaining if you compare two pointers that don't
point to the same type.

Gr{oetje,eeting}s,

            Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@xxxxxxxxxxxxxx

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
             Â Â -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/