I use this preprocessor macro to βplanβ and easily return from the definition definition function:
#define STRINGIFY_RETURN(x) case x: return
It works like a charm in an MBSC environment with regular string literals. Example:
#define MY_DEFINE_1 1 #define MY_DEFINE_2 2 #define MY_DEFINE_3 3 const char* GetMyDefineNameA(unsigned int value) { switch(value) { STRINGIFY_RETURN(MY_DEFINE_1); STRINGIFY_RETURN(MY_DEFINE_2); STRINGIFY_RETURN(MY_DEFINE_3); default: return "Unknown"; } }
However, I have increasingly had to switch to Unicode compatibility, so I had to rewrite this function to return Unicode strings that require the L prefix before string literals. So I tried:
#define STRINGIFY_RETURN_WIDE(x) case x: return #x L"" const wchar_t* GetMyDefineNameW(unsigned int value) { switch(value) { STRINGIFY_RETURN_WIDE(MY_DEFINE_1); STRINGIFY_RETURN_WIDE(MY_DEFINE_2); STRINGIFY_RETURN_WIDE(MY_DEFINE_3); default: return L"Unknown"; } }
But this gives me errors:
error C2308: concatenation of inconsistent strings
error C2440: 'return': cannot convert from 'const char [12]' to 'const wchar_t *
I also tried:
#define STRINGIFY_RETURN_WIDE(x) case x: return L #x "" #define STRINGIFY_RETURN_WIDE(x) case x: return #x "" L
but no matter, I can't get it to work. I do not know about this and cannot find a solution.
I would be very grateful if someone could show the correct way to make this macro so that it allows a Unicode string literal.
Update:
#define STRINGIFY_RETURN_WIDE(x) case x: return L
does not throw error C2440, but it still gives me C2308.
Update 2:
I am using Microsoft Visual Studio 2013
c ++ c macros c-preprocessor stringification
Vinzenz
source share