c - Type-generic programming with macros: tricks to determine type? -


it's possible types of type-generic-functions macros in c, instance things like:

#define sqrt(x) (sizeof(x) == sizeof(float) ? sqrtf((x)) : \                  sizeof(x) == sizeof(double) ? sqrt((x)) : \                  sqrtl((x)) ) 

this works (mostly) expected long x floating point type.

but if want type-generic macro can take either integer type or pointer type, might have same size. there clever way test whether macro argument integer or pointer? integer versus floating point type?

no. macros not know types are. perform literal copy-and-paste of #define. type safety doesn't exist here.

c not typed language in meaningful sense. if want modicum of type safety, use c++, can accomplish little templates , function overloading.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -