Kasper Dupont wrote:
> I don't have a solution for this problem, but I know
> that gcc have some features that might come in handy.
>
it can be found from gcc manual...
if (__builtin_types_compatible_p (typeof (x), int ))
my_int_func (x);
else if (__builtin_types_compatible_p (typeof (x), float ))
my_float_func (x);
> It can do type checking of printf statements as long
> as the format string is a constant. But that doesn't
> seem general enough to apply in your case.
>
> I tried to come up with something using typeof, a
> bit of macro magic (including the # operator), nested
> functions, and finally __PRETTY_FUNCTION__.
> Unfortunately it seems __PRETTY_FUNCTION__ contains
> only the necesarry information if you are using C++,
> and there you don't have nested functions.
>
> The closest I could come up with was a switch on
> sizeof. But if two of the types you might be using
> are of the same size, then that isn't of much help.
>
>
> Here is the code. Can somebody suggest a fix? Or is
> this really impossible?
>
> #include <stdio.h>
> #define config_write(key, value) do { \
> char *__foobar (typeof(value) x) { \
> return __PRETTY_FUNCTION__; \
> } \
> do_config_write(key,__foobar(value),value);\
> } while(0)
> void do_config_write(long key, char *type, ...)
> {
> printf("%ld: %s\n",key,type);
> }
> int main()
> {
> int foobar=42;
> config_write(27,foobar);
> return 0;
> }
>
|