如何挑选出发送到仅采用可变参数的宏的第一个参数
我尝试获取发送到可变参数宏的第一个实际参数。这是我尝试过的,但在 VS2010 中不起作用:
#define FIRST_ARG(N, ...) N
#define MY_MACRO(...) decltype(FIRST_ARG(__VA_ARGS__))
当我查看预处理器输出时,我看到 FIRST_ARG 返回发送到 MY_MACRO 的整个参数列表...
另一方面,当我尝试使用:时,
FIRST_ARG(1,2,3)
它会按预期扩展为 1。
这似乎与臭名昭著的两级连接宏所解决的问题相反。我知道“宏参数在插入宏体之前已完全展开”,但这似乎对我没有帮助,因为我不明白这在...和 __VA_ARGS__ 的上下文中意味着什么。
显然 __VA_ARGS__
绑定到 N
并且仅在稍后进行评估。我尝试了几种具有额外宏观级别的方法,但没有帮助。
I try to get at the first actual parameter sent to a variadic macro. This is what I tried, and which does not work in VS2010:
#define FIRST_ARG(N, ...) N
#define MY_MACRO(...) decltype(FIRST_ARG(__VA_ARGS__))
When I look at the preprocessor output I see that FIRST_ARG
returns the entire argument list sent to MY_MACRO
...
On the other hand when I try with:
FIRST_ARG(1,2,3)
it expands to 1 as intended.
This seems to be somehow the inverse of the problem solved by the infamous two level concat macros. I know that "macro parameters are fully expanded before inserted in the macro body" but this does not seem to help me here as I don't understand what this means in the context of ... and __VA_ARGS__
Obviously __VA_ARGS__
binds to N
and is only evaluated later. I tried several ways with extra macro levels but it didn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是Visual C++ 预处理器中的错误。那里列出的解决方法有效。这:
扩展到:
This is a bug in the Visual C++ preprocessor. The workaround listed there works. This:
expands to: