MSVC无法正确扩展__VA_ARGS__ [英] MSVC doesn't expand __VA_ARGS__ correctly

查看:285
本文介绍了MSVC无法正确扩展__VA_ARGS__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
#define G(...) F(__VA_ARGS__)
F(1, 2, 3)
G(1, 2, 3)

两个宏的预期输出为X = 1 and VA_ARGS = 2, 3,这就是我在GCC中获得的结果,但是,MSVC将其扩展为:

The expected output is X = 1 and VA_ARGS = 2, 3 for both macros, and that's what I'm getting with GCC, however, MSVC expands this as:

X = 1 and VA_ARGS = 2, 3
X = 1, 2, 3 and VA_ARGS =

也就是说,__VA_ARGS__被扩展为单个参数,而不是分解为多个参数.

That is, __VA_ARGS__ is expanded as a single argument, instead of being broken down to multiple ones.

有什么办法解决吗?

推荐答案

MSVC的预处理器的行为似乎与标准行为大不相同. 规格.
以下解决方法可能会有所帮助:

MSVC's preprocessor seems to behave quite differently from the standard specification.
Probably the following workaround will help:

#define EXPAND( x ) x
#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
#define G(...) EXPAND( F(__VA_ARGS__) )

这篇关于MSVC无法正确扩展__VA_ARGS__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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