您的位置:首页 > 其它

How to convert concatenated strings to wide-char with the C preprocessor?

2012-05-25 13:12 459 查看
If you want to convert a character string literal to a wide character string literal, you properly define a macro as _L here:
[align=left] [/align]
[align=left]#define _L(x) __L(x)[/align]
[align=left]#define __L(x) L ## x[/align]
[align=left]#define STR "I am handsome"[/align]
[align=left] [/align]
[align=left]wchar_t str1[] = _L("I am handsome" );[/align]
[align=left]wchar_t str2[] = _L(STR);[/align]

It works fine at most all compiler currently. However, when use it to convert the concatenated string like

[align=left]#define STR3 "I am handsome\n" \[/align]
[align=left] "I love u\n"[/align]

[align=left] [/align]

[align=left]wchar_t str3[] = _L(STR3);//error C2308: concatenating mismatched strings[/align]

you will get a compiler error in MSVC.
This is because it's the new feature in C99 standards. The MSVC don't support it for a long time.
If uses the GCC, the second code piece get compiled without error.

Look how C99 standard said.
[align=left]6.4.5 String literals[/align]

[align=left]In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any f the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: