I would like to do the equivalent of the following:
#define print_max(TYPE) \
# ifdef TYPE##_MAX \
printf("%lld\n", TYPE##_MAX); \
# endif
print_max(INT);
Now the #ifdef
or any nested preprocessor directive is
not allowed as far as I can see in a function macro.
Any ideas?
Update: So it seems like this is not possible. Even a hack to check at runtime seems unachievable. So I think I'll go with something like:
#ifndef BLAH_MAX
# define BLAH_MAX 0
#endif
# etc... for each type I'm interested in
#define print_max(TYPE) \
if (TYPE##_MAX) \
printf("%lld\n", TYPE##_MAX);
print_max(INT);
print_max(BLAH);
Unlike templates, the preprocessor is not turing-complete. An
#ifdef
inside a macro is not possible. Your only solution is to make sure you only callprint_max
on types which has a matching_MAX
defined, e.g.INT_MAX
. The compiler will surely tell you when they aren't.The only solution I have is cheating - produce a list of types that have an _XXX_MAX as a set of defines, and then use that. I don't know how to produce the list in automated fashion in preprocessor, so I don't try. The assumption is that the list is not too long and will not be maintained too intensively.
There's no easy way to do this. The closest you can come is to #define a large number of IFDEF macros such as:
since you need a lot of them (and they might be useful multiple places), it makes a lot of sense to stick all these in their own header file 'ifdefs.h' which you can include whenever you need them. You can even write a script that regenerates ifdef.h from a list of 'macros of interest'
Then, your code becomes
As long as you're only interested in integral values, and assuming hardware using 2's compliment and 8-bit bytes:
I've tried that before. The problem is that
#
is already reserved to stringize a macro parameter. It isn't parsed as a preprocessor token like the one in#
define.I don't think it's a case of the ## operator not being allowed in an #ifdef. I tried this:
and it still didn't work (it didn't like #ifdef TYPE). The problem is that #ifdef will only accept #defined symbols, not #define arguments. Those are two different things.