c - Type overloading macro -
i have bunch of printf debug helper macros , pretty cool have not specify type, there can allow macro overloading in c(can gcc specific if available in gcc 4.3). thought maybe typeof apparently doesn't work.
example macro(i have ascii terminal color stuff can't remember of top of head)
#ifdef _debug #define dprint_int(x) printf("int %s equal %i @ line %i",#x,x,__line__); . . . #else #define dprint_int(x) . . . #endif
try this; uses gcc's __builtin methods, , automatically determines type you, best can, , makes easy debug macro don't have specify type. of course, can compare typeof (x) float, etc. etc.
#define debug(x) \ ({ \ if (__builtin_types_compatible_p (typeof (x), int)) \ fprintf(stderr,"%d\n",x); \ else if (__builtin_types_compatible_p (typeof (x), char)) \ fprintf(stderr,"%c\n",x); \ else if (__builtin_types_compatible_p (typeof (x), char[])) \ fprintf(stderr,"%s\n",x); \ else \ fprintf(stderr,"unknown type\n"); \ })
Comments
Post a Comment