How can such a macro be expanded and strengthened?

I am creating a wrapper for a string parser that converts a string to primitive types. I'm writing unit tests for it, so I need a string representation of the extreme values โ€‹โ€‹of these primitive types, so standard macros like INT_MIN and INT_MAX need to be rolled up.

Normal string macros look like this:

#define STRINGIFY(content) #content
#define EXPAND_AND_STRINGIFY(input) STRINGIFY(input)

      

It works well for EXPAND_AND_STRINGIFY(SHRT_MAX)

to be expanded to "32767".

However, when it works with EXPAND_AND_STRINGIFY(SHRT_MIN)

, it will be expanded to "(-32767 -1)" because #define SHRT_MIN (-SHRT_MAX - 1)

. This is not what I want. Are there possible workarounds?

+3


source to share


1 answer


No, there is no way to get a preprocessor macro to evaluate an arithmetic expression.

You can force the preprocessor to do arithmetic evaluation, but only in a context #if

that allows you to evaluate a boolean expression. You can use this function for a rather tedious output of a number, but only using preprocessor input in the #include

d files , as you cannot put #if

inside a macro.



You don't mention why you want to shrink INT_MIN

, so I can't suggest an alternative if one actually exists.

However, most likely your best bet is to simply create the string at runtime using snprintf

.

+4


source







All Articles