&是什么QUOT; STR的#define(一)#A"做? [英] What does "#define STR(a) #a" do?

查看:126
本文介绍了&是什么QUOT; STR的#define(一)#A"做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读音素的源$ C ​​$ C。这是一个开放源码软件的JavaME实现。它是用C ++编写,我偶然发现了这一点:

I'm reading the phoneME's source code. It's a FOSS JavaME implementation. It's written in C++, and I stumbled upon this:

// Makes a string of the argument (which is not macro-expanded)
#define STR(a) #a

我知道C和C ++,但我从来没有读过这样的事情。什么是#A 吗?

另外,在同一文件中,有:

Also, in the same file, there's:

// Makes a string of the macro expansion of a
#define XSTR(a) STR(a)

我的意思是,有什么用定义一个新的宏,如果它是所有调用现有的宏?

I mean, what's the use of defining a new macro, if all it does is calling an existing macro?

源$ C ​​$ c是在<一个href=\"https://phoneme.dev.java.net/source/browse/phoneme/releases/phoneme_feature-mr2-rel-b23/cldc/src/vm/share/utilities/GlobalDefinitions.hpp?rev=5525&view=markup\">https://phoneme.dev.java.net/source/browse/phoneme/releases/phoneme_feature-mr2-rel-b23/cldc/src/vm/share/utilities/GlobalDefinitions.hpp?rev=5525&view=markup.你可以用CTRL + F查找。

The source code is in https://phoneme.dev.java.net/source/browse/phoneme/releases/phoneme_feature-mr2-rel-b23/cldc/src/vm/share/utilities/GlobalDefinitions.hpp?rev=5525&view=markup. You can find it with a CTRL+F.

推荐答案

在第一个定义,#A 表示打印宏参数是一个字符串。这将开启,例如 STR(富)foo的,但它不会做它的参数宏扩展。

In the first definition, #a means to print the macro argument as a string. This will turn, e.g. STR(foo) into "foo", but it won't do macro-expansion on its arguments.

第二个定义不添加任何东西到了第一,但通过传递它的参数到另一个宏,它迫使其参数的全宏扩展。因此, XSTR(表达式)创建 EXPR 与完全展开所有的宏字符串。

The second definition doesn't add anything to the first, but by passing its argument to another macro, it forces full macro expansion of its argument. So XSTR(expr) creates a string of expr with all macros fully expanded.

这篇关于&是什么QUOT; STR的#define(一)#A&QUOT;做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆