这个宏包装展示了什么概念? [英] What concept is being exhibited in this macro wrapping?
问题描述
一堆代码刚刚交给我,我对标头中的宏感到困惑.我不明白它们的用途:
A bunch of code just got handed over to me and I got baffled by macros in the header. I could not understand what they are for:
#define WRAPPER_MACRO(symbol) symbol
#define ANOTHER_SYMBOL WRAPPER_MACRO(ANOTHER_SYMBOL)
#define PREFIXED_ANOTHER_SYMBOL WRAPPER_MACRO(PFX_ANOTHER_SYMBOL)
为什么这样做?有什么好处?
Why do this? What is the benefit?
这不是我代码库中的实际逐字代码,但它具有相同的模板.我只是替换了宏名.
This is not the actual verbatim code from my codebase but it has the same template. I just replaced macro names.
推荐答案
正如@Michael所说,我们需要确定真正的宏才能知道.但是,如果没有它们,我愿意做出一些可能会帮助您的猜测.
As @Michael said, we'll need to see the real macros to know for sure. But without them, I'm willing to take a few guesses that might help you out.
宏嵌套可能是字符串化.这部分代码来自我维护的代码库:
The macro nesting is probably a stringification thing. This bit of code is from a codebase I maintain:
// As per http://gcc.gnu.org/onlinedocs/cpp/Stringification.html:
// "If you want to stringify the result of expansion of a macro argument, you
// have to use two levels of macros."
#ifndef STRINGIFY
#define STRINGIFY(s) TOSTRING(s)
#define TOSTRING(s) #s
#endif
我还猜测您的PREFIXED_ANOTHER_SYMBOL
宏正在执行与此类似的操作,使用#
或##
预处理程序指令将某个符号添加到您喂入宏的任何内容之前.
I'm also guessing your PREFIXED_ANOTHER_SYMBOL
macro is doing something similar to this, using the #
or ##
preprocessor directives to prepend a certain symbol to whatever you feed the macro.
这篇关于这个宏包装展示了什么概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!