Combining wide string literal with string macro
Asked Answered
T

2

5

I have a macro for a character string as follows:

#define APPNAME "MyApp"

Now I want to construct a wide string using this macro by doing something like:

const wchar_t *AppProgID = APPNAME L".Document";

However, this generates a "concatenating mismatched strings" compilation error.

Is there a way to convert the APPNAME macro to a wide string literal?

Throwback answered 6/11, 2009 at 20:13 Comment(1)
Note: C++0x has a new "do the right thing" rule for this case. §2.14.5/13: "If one string literal has no encoding-prefix, it is treated as a string literal of the same encoding-prefix as the other operand."Tann
F
12

Did you try

#define APPNAME "MyApp"

#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)

const wchar_t *AppProgID = WIDEN(APPNAME) L".Document";
Fran answered 6/11, 2009 at 20:17 Comment(2)
Yes, but I was wondering if there was a way of doing this without having to define two versions of the macro (wide and non-wide).Throwback
I've updated my answer to have use some common preprocessor technique to deal with strings. You can see more advanced usages if you peek at boost.org/doc/libs/1_40_0/libs/preprocessor/doc/index.htmlFran
C
0

Without macros:

const wchar_t *AppProgID = L"" APPNAME ".Document";
Citrine answered 23/1, 2022 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.