Working with a unit test framework, I came across a situation in which I'd like to test macro arguments. Simply said, I'd like to expand the macro FOO(x)
such that FOO(int)
would be short
and FOO(anything_else)
would be long
.
With C++ templates, of course this isn't a problem. But here I need a real token replacement, not just a typedef
. I.e. FOO(char) FOO(char) i;
should be a valid definition equal to long long i;
.
As far as I know, the only string-like operations available in C macros are pasting/concatenating tokens (using ##
), and string-izing them (using #
).
I'm pretty sure the closest you're going to get involves enumerating the possibilities like so:
#define FOO(x) FOO__##x
#define FOO__int short
#define FOO__short long
#define FOO__long long
#define FOO__char long
// ... for each type you want to replace
Inspiration from this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With