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

If you want to convert a character string literal to a wide character string literal, you properly define a macro as _L here:
 
#define _L(x)  __L(x)
#define __L(x) L ## x
#define STR "I am handsome"
 
wchar_t str1[] = _L("I am handsome" );
wchar_t str2[] = _L(STR);
 
It works fine at most all compiler currently. However, when use it to convert the concatenated string like
#define STR3 "I am handsome\n" \
             "I love u\n"
 
wchar_t str3[] = _L(STR3);//error C2308: concatenating mismatched strings
 
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.
6.4.5 String literals
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.
posted @   嗷嗷  阅读(414)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示