cub*_*l42 9 c c++ string macros c-preprocessor
C 预处理器具有称为stringification的功能。这是一项允许从宏参数创建(窄)字符串文字的功能。它可以像这样使用:
#define PRINTF_SIZEOF(x) printf("sizeof(%s) == %d", #x, sizeof(x))
/* stringification ^^ */
Run Code Online (Sandbox Code Playgroud)
用法示例:
PRINTF_SIZEOF(int);
Run Code Online (Sandbox Code Playgroud)
...可能会打印:
sizeof(int) == 4
Run Code Online (Sandbox Code Playgroud)
如何从宏参数创建宽字符串文字?换句话说,我该如何实施WPRINTF_SIZEOF?
#define WPRINTF_SIZEOF(x) wprintf( <what to put here?> )
Run Code Online (Sandbox Code Playgroud)
为了从宏参数生成宽字符串文字,您需要将 stringification 与concatenation结合起来。
WPRINTF_SIZEOF 可以定义为:
#define WPRINTF_SIZEOF(x) wprintf(L"sizeof(%s) == %d", L ## #x, sizeof(x))
/* concatenation ^^ ^^ stringification */
Run Code Online (Sandbox Code Playgroud)
为了(可以说)提高可读性,您可以将此技巧提取到辅助宏中:
#define WSTR(x) L ## #x
#define WPRINTF_SIZEOF(x) wprintf(L"sizeof(%s) == %d", WSTR(x), sizeof(x))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1307 次 |
| 最近记录: |