Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I conditionally replace preprocessor arguments?

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;.

like image 475
MSalters Avatar asked Jun 19 '12 14:06

MSalters


1 Answers

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.

like image 63
vergenzt Avatar answered Sep 28 '22 04:09

vergenzt