Listing
Listing

Reputation: 1201

Macro long string concernation

In my application I want to add the version ID as a macro and use it in multiple parts of the application. As explained in this question I can easily generate a string with this:

#define APP_VER "1.0"
#define APP_CAPTION "Stackoverflow example app v." ## APP_VER

My problem is now, that in some parts, I need to have the caption as an unicode string.

I tried the following:

MessageBoxW(0,_T(APP_CAPTION),L"Minimal Counterexample",0);

But it gives the error "can't concernate wide 'Stackoverflow example app v.' with narrow '1.0'"

I also tried

#define WIDE_CAPTION L ## APP_CAPTION

But that just gives "LAPP_CAPTION" is not defined.

I know that I can convert the string at runtime to unicode, but that is rather messy. Can someone provide a Macro-level solution for my problem?

Upvotes: 1

Views: 162

Answers (1)

You just want:

#define APP_CAPTION "Stackoverflow example app v." APP_VER

Since APP_VER is already a string.

String concatenation happens for free, for example:

const char *str = "hello " "world"

Complete compilable example:

#include <iostream>
#define APP_VER "1.0"
#define APP_CAPTION "Stackoverflow example app v." APP_VER

int main() {
  std::cout << APP_CAPTION << "\n";
  return 0;
}

Upvotes: 2

Related Questions