以 C11 _Generic 为例(必须填写完整检查):
#define DEBUG_INT(a, b) DEBUG("%d %d?", (a), (b))
#define DEBUG_DOUBLE(a, b) DEBUG("%f %f?", (a), (b))
#define FAIL(a, b) assert(0)
#define max(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_Generic((_a), int: DEBUG_INT, double: DEBUG_DOUBLE, default: FAIL))(_a, _b); \
_a > _b ? _a : _b; })
或使用__builtin_types_compatible_p gcc 扩展:
#define max(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
if (__builtin_types_compatible_p(typeof(int), _a) && __builtin_types_compatible_p(typeof(int), _a)) DEBUG_INT(_a, _b); \
else if (__builtin_types_compatible_p(typeof(double), _a) && __builtin_types_compatible_p(typeof(double), _a)) DEBUG_DOUBLE(_a, _b); \
else FAIL(_a, _b);
_a > _b ? _a : _b; })