如何单独输出发送到宏的第一个参数,只取一个可变参数 [英] How to single out the first parameter sent to a macro taking only a variadic parameter

查看:385
本文介绍了如何单独输出发送到宏的第一个参数,只取一个可变参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取发送到可变宏的第一个实际参数。这是我试过,它不工作在VS2010:

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__))

在预处理器输出我看到 FIRST_ARG 返回发送到 MY_MACRO ...的整个参数列表...

When I look at the preprocessor output I see that FIRST_ARG returns the entire argument list sent to MY_MACRO...

另一方面,当我尝试:

FIRST_ARG(1,2,3)

它会按预期展开为1。

这似乎是由臭名昭着的两级连接宏解决的问题的反向。我知道宏参数在插入宏体之前完全展开,但这似乎并不帮助我在这里,因为我不明白这是什么意思在上下文中...和 __ VA_ARGS __

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__

显然 __ VA_ARGS __ 绑定到 N 并且仅在稍后进行评估。

Obviously __VA_ARGS__ binds to N and is only evaluated later. I tried several ways with extra macro levels but it didn't help.

推荐答案

这是 Visual C ++预处理器中的错误。列出的解决方法有效。这:

This is a bug in the Visual C++ preprocessor. The workaround listed there works. This:

#define FIRST_ARG_(N, ...) N
#define FIRST_ARG(args) FIRST_ARG_ args
#define MY_MACRO(...) decltype(FIRST_ARG((__VA_ARGS__)))

MY_MACRO(x, y, z)

扩展为:

decltype(x)

这篇关于如何单独输出发送到宏的第一个参数,只取一个可变参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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