I need to convert defined constants to their integer values so I came up with this macro (STR_TO_CONST) for doing so...
#define STRFY_VAL(s) #s
#define STRFY_KEY(s) STRFY_VAL(s)
#define STR_TO_CONST(s) atoi(STRFY_KEY(s))
It works, but I'm wondering if there are any potential problems with it as I've never encountered this macro before despite having searched considerably for something like it.
The reason you never encountered this is that it's utterly pointless, but let's explain by example. Say you have the following:
now, this is semantically no different from:
That's because preprocessor macros are ultimately just textual replacement happening before you actually compile. Therefore,
FINALANSWERis just as good as an integer constant as42is.Your "solution" to a non-existing problem just adds overhead in that it adds a new string constant to your code and an unnecessary function call as well.