带有“占位符”的宏价值
我正在使用一个包含一组预处理器库的库。其中之一是 FOR_EACH 样式宏,它迭代 __VA_ARGS__ 并为每个参数调用用户提供的宏。用户提供的宏的调用方式如下:SOME_MACRO(current_arg)
但是,问题是它仅适用于采用单个参数的用户提供的宏。我正在尝试做一些特殊的事情,其中涉及结构的名称和结构中的每个字段。问题是,这需要宏的两个参数。
由于我正在使用的库仅接受一元宏,因此是否有某种方法可以将附加参数“绑定”到我的宏?
截至目前,我必须在宏中对结构的名称进行硬编码。因此,如果我正在使用的 struct
名为 Foo
,我不得不说:
#define MY_MACRO(FIELD) /* do something with &Foo::FIELD */
是否可以通过某种方式“绑定”第二个 STRUCT
> 宏的参数,也许有一些进一步的间接,这样当库调用我的宏时,它就可以扩展为:
#define MY_MACRO(FIELD) /* do something with &STRUCT::FIELD */
I'm working with a library that includes a set of preprocessor libraries. One of them is a FOR_EACH style macro which iterates over a __VA_ARGS__
and calls a user-provided macro for each argument. The user provided macro is called like: SOME_MACRO(current_arg)
However, the problem is that it only works with user-provided macros that take a single argument. I'm trying to do something special which involves both the name of a struct
and each field in the struct. The problem is, this requires two arguments to the macro.
Since the library I'm working with only accepts a unary macro, is there some way to "bind" an additional argument to my macro?
As of now, I have to hardcode the name of the struct in my macro. So, if the struct
I'm working with is named Foo
, I have to say:
#define MY_MACRO(FIELD) /* do something with &Foo::FIELD */
Is there someway I could "bind" a second STRUCT
argument to the macro, perhaps with some further indirection, so that when the library invokes my macro it would be able to expand as:
#define MY_MACRO(FIELD) /* do something with &STRUCT::FIELD */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。您可以使用以下技术。
下面的测试代码中的用法:
上面的代码扩展为,
Yes. You can use following technique.
Usage in the below test code:
Above code is expanded to,