tags:

views:

68

answers:

1

I want to concatenate two wide strings using a macro, so I define some macros:

#define VERSION_MAJOR 1
#define VERSION_MINOR 1
#define VERSION_BUILD 0
#define VERSION_REVISION 0


#define _STR(s) #s
#define STR(s) _STR(s)

#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)


//http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
#define PRODUCT_ELASTOS_VERSION STR(VERSION_MAJOR) "." \
                                STR(VERSION_MINOR) "." \
                                STR(VERSION_BUILD) "." \
                                STR(VERSION_REVISION)

now I want to define a new macro PRODUCT_ELASTOS_VERSION_W using macro PRODUCT_ELASTOS_VERSION, it's value should be L"1.1.0.0". so how can I define this macro? TOWSTRING(PRODUCT_ELASTOS_VERSION) is wrong answer.

And if I want to concatenating string, how should I write? L"v" TOWSTRING(PRODUCT_ELASTOS_VERSION) cann't get wide string L"v1.1.0.0".

A: 

First, PRODUCT_ELASTOS_VERSION does not expand to "1.1.0.0", it expands to

"1" "." "1" "." "0" "." "0"

Keeping the same structure you can define another identifier that expands to

L"1" L"." L"1" L"." L"0" L"." L"0"

with

#define _LSTR(s) L ## #s
#define LSTR(s) _LSTR(s)

#define ANOTHER_IDENTIFIER LSTR(VERSION_MAJOR) L"." \
                           LSTR(VERSION_MINOR) L"." \
                           LSTR(VERSION_BUILD) L"." \
                           LSTR(VERSION_REVISION)
pmg
It's not the best way, because if I define PRODUCT_NAME "hello wold", then wprintf(L"%s\n", L"v" TOWSTRING(PRODUCT_NAME)) works ok, but wprintf(L"%s\n", TOWSTRING(PRODUCT_VERSION)) is wrong.I want that each macro can works in the same way.
xufan