预处理器宏:如何插入参数?
该代码有许多以下部分:
int filter;
#ifdef INPUTFILTER_FOO
LOG4CXX_DEBUG(log, "FOO filter used");
filter = F_FOO;
#endif
它们在代码中多次使用(用于为所有测试配置提供 I/O、线程支持等),有时它们对于调试至关重要,但使代码看起来很粗糙,想要替换它们带有宏,每个category_type命名空间都有一个宏。
因此,想要扩展以下内容:
MACROSTUFFBAZ(log2, stuff, "BAZ")
<- 文本部分对于每个类都是唯一的,因此它也需要包含在宏中。
to:
#ifdef INPUTSTUFF_BAZ
LOG4CXX_DEBUG(log2, "BAZ stuff used");
stuff = S_BAZ;
#endif
要定义宏,计划使用这个:
debug.hpp:(
#ifdef INPUTSTUFF_BAZ
#define MACROSTUFFBAZ ...
#else
#define MACROSTUFFBAZ
.. no code!
#endif
#endif
至少这将给出当前正在试用的事物的清晰概述,而无需在代码中看到它们)
The code has a number of following sections:
int filter;
#ifdef INPUTFILTER_FOO
LOG4CXX_DEBUG(log, "FOO filter used");
filter = F_FOO;
#endif
They are used multiple times in the code (used to provide I/O, threading support etc for all testing configurations), Circa they are essential for debugging but make the code look harsh, want to replace them with macros, one for each category_type namespace.
So, want to expand the following:
MACROSTUFFBAZ(log2, stuff, "BAZ")
<- the text part is unique for each class, so it needs to be included in macro too.
to:
#ifdef INPUTSTUFF_BAZ
LOG4CXX_DEBUG(log2, "BAZ stuff used");
stuff = S_BAZ;
#endif
To define macros, plan to use this:
debug.hpp:
#ifdef INPUTSTUFF_BAZ
#define MACROSTUFFBAZ ...
#else
#define MACROSTUFFBAZ
.. no code!
#endif
#endif
(at least this will give a clear overview of the things currently undergoing probation, without seeing them around the code)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的尝试,尽管我不能 100% 确定它是否有效,因为我现在无法测试它,而且不同的编译器处理预处理器宏的方式也略有不同。但我认为这样的东西至少在视觉工作室中有效。
基本上,您必须使用辅助宏将参数转换为字符串。其次,您可以使用
##
连接标识符:edit: 这里实际上不需要辅助宏,因此您可以直接输入
#C
直接在 MACROSTUFFBAZ 的定义中。Here's my try, although i'm not 100% sure if it works cause i'm unable to test it right now, and also different compilers handle preprocessor macros a little bit differently. But I think something like this works at least in visual studio.
Basically, you have to use a helper macro to convert the parameter into a string. And secondly, you can use
##
to concatenate identifiers:edit: The helper macro is actually not needed here, so you can just put
#C
directly in the definition of MACROSTUFFBAZ.