I have code that mostly needs Unicode strings, but I want to make it conditional (i.e. TEXT("string") expands to L"string" or "string" , depending on the settings). For this, I use a macro:
#ifdef _UNICODE # define VSTR(str) L##str #else # define VSTR(str) str #endif
The main complication of this is printf format strings, which use %s and %s for strings with the same encoding and different encodings, respectively. Some strings come from similar conditional APIs ( TCHAR and similar), while some of them relate to a set of APIs (mainly C strings). When using _tprintf and the family, the function used may change, making the conditions %s and %s conditional, and they may need to be reversed. To handle this, I defined macros for the corresponding format elements:
#ifdef _UNICODE # define VPFCSTR(str) "%S" # define VPFWSTR(str) "%s" # define VPFTSTR(str) VPFWSTR(str) #else # define VPFCSTR(str) "%s" # define VPFWSTR(str) "%S" # define VPFTSTR(str) VPFCSTR(str) #else
Now everything works fine, but the syntax forces:
VSTR("Beginning of a format string, with a string '") VPFTSTR VSTR("' included.")
I would like to be able to use syntax like:
VSTR("Beginning of a format string, with a string '", VPFTSTR, "' included.")
For Unicode, this needs to be expanded to:
L"Beginning of a format string, with a string '" L"%s" L"' included."
The only complication is a variable number of arguments, all of which must be converted in the same way (as needed).
My first idea was to use __VA_ARGS__ to handle this using empty arguments, like this:
VASTR(str, ...) VSTR(str) VASTR(__VA_ARGS__)
Unfortunately, since macros cannot be used in their own definition, this fails. Then I tried the proxy:
VASTR2(...) VASTR(__VA_ARGS__) VASTR(str, ...) VSTR(str) VASTR2(__VA_ARGS__)
The proxy method also does not work.
Is there a way to handle running the same macro for each macro argument (zero) that takes a variable number of arguments? Or, if not, is there an equivalent? If specific to the compiler, MSVC10 is preferred, but all of this is of interest.