I'm using this preprocessor macro to "stringify" and return easily from a definition resolving function:
#define STRINGIFY_RETURN(x) case x: return #x ""
It works like a charm in MBSC environment with normal 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 had to switch to Unicode compatibility more and more and so I had to rewrite this function to return Unicode strings which require the prefixing with L
in front of the 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 that gives me the errors:
error C2308: concatenating mismatched 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 what, I can't get it to work. I'm clueless about this and can't seem to find a solution.
I'd be really grateful if someone could show the correct way to do this macro so that it resolves to a Unicode string literal.
Update:
#define STRINGIFY_RETURN_WIDE(x) case x: return L#x ""
does not throw the C2440 error, but it still gives me the C2308.
Update 2:
I'm using Microsoft Visual Studio 2013
return L#x
should be sufficient, I think. – Abbate