Friday, January 7, 2011

C Macro Tips and Tricks

Mike Ash:

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 produces 1 if the two types are “compatible” (roughly, if they’re equal) and 0 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.

Comments RSS · Twitter

Leave a Comment