çpreprocessor字面解释 [英] C preprocessor Literal Construction
本文介绍了çpreprocessor字面解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是:
我有一个字符串文字是宏观ED像这样
I have a string literal that is macro-ed like so
#define TITLE "Title"
但也有这样的情况,我需要在此字符串的一个宽字符变种通过。我希望能够通过 L标题
这些功能。所以很自然,我开始尝试在标题
的角度来定义一个新宏 W_TITLE
。
But there are instances when I need to pass in a wide char variant of this string. I want to be able to pass L"Title"
to those functions. So naturally, I set out trying to define a new macro W_TITLE
in terms of TITLE
.
但我有没有运气,我所有的方法(下面进行说明)都失败了。请告诉我这种法宝怎么得来的。
But I have no luck, all my approaches (listed bellow) have failed. Please tell me how such magic can be accomplished.
我试过
#define W_TITLE L##TITLE
#define W_TITLE #L TITLE
#define W_TITLE ##L TITLE
但他们都失败...
But they all fail...
推荐答案
试试这个:
#define WIDEN_(exp) L##exp
#define WIDEN(exp) WIDEN_(exp)
#define TITLE "Title"
#define W_TITLE WIDEN(TITLE)
您需要强制的扩展,通过一个中间的宏让你在找什么。
You need to force an expansion through an intermediate macro to get what you're looking for.
#include <stdio.h>
#define WIDEN_(exp) L##exp
#define WIDEN(exp) WIDEN_(exp)
#define TITLE "Title"
#define W_TITLE WIDEN(TITLE)
int main(int argc, char *argv[])
{
printf("%s\n", TITLE);
wprintf(L"%ls\n", W_TITLE);
return 0;
}
结果:
Title
Title
这篇关于çpreprocessor字面解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文