4

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?

a3f
  • 8,517
  • 1
  • 41
  • 46
flashk
  • 2,451
  • 2
  • 20
  • 31
  • 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." โ€“ Potatoswatter Aug 09 '11 at 11:22

2 Answers2

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";
Marcel Gosselin
  • 4,610
  • 2
  • 31
  • 54
  • 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). โ€“ flashk Nov 06 '09 at 20:19
  • 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 http://www.boost.org/doc/libs/1_40_0/libs/preprocessor/doc/index.html โ€“ Marcel Gosselin Nov 06 '09 at 20:39
0

Without macros:

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