C Macro Tips and Tricks
gcc
provides two built-in functions which can be useful for building macros.The first is
__builtin_types_compatible_p
. You pass two types to this function (__typeof__
comes in handy here) and it produces1
if the two types are “compatible” (roughly, if they’re equal) and0
if they aren’t.The second is
__builtin_choose_expr
. This works like the C standard?:
operator, except that the predicate must be a compile-time constant, and the type of the entire__builtin_choose_expr
expression is the type of whichever branch gets chosen; the two branches are not required to be similar types.
A good compilation of notes.