I am working on a project where I have many constant strings formed by concatenation (numbers, etc.).
For example, I have a LOCATION
macro that formats __FILE__
and __LINE__
into a string that I can use to know where I am in the code, when printing messages or errors:
#define _STR(x) # x
#define STR(x) _STR(x)
#define LOCATION __FILE__ "(" STR(__LINE__) ")"
So, this would format a location like "file.cpp(42)". The problem is when I try to convert the result to a wide-string:
#define _WIDEN(x) L ## x
#define WIDEN(x) _WIDEN(x)
#define WLOCATION WIDEN(LOCATION)
This works just fine with GCC, and results in L"file.cpp(42)" being inserted in my code. However, when trying this with MSVC++ (using Visual C++ 2008 Express), I get an error:
error: Concatenating wide "file.cpp" with narrow "("
I understand that the L
prefix gets added only to the first term in my expression. I've also tried this:
#define _WIDEN(x) L ## #x
Which "works", but gives the string L""file.cpp" "(" "42" ")""
which is obviously not very convenient (and not what I am looking for), especially considering that this macro is simple compared to other macros.
So, my question is: how can I get it to apply to the entire expression in MSVC++, so I can get the same result I am getting with GCC? I would rather not create a second string with all-wide tokens, because I would then have to maintain two macros for each one, which is not very convenient and can lead to bugs. Plus, I need the narrow version of each string as well, so using all-wide strings is not an option either, unfortunately.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…