MSVC 没有正确扩展 __VA_ARGS__ [英] MSVC doesn't expand __VA_ARGS__ correctly

查看:30
本文介绍了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 和 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.

有什么办法可以解决这个问题吗?

Any way around this?

推荐答案

这个问题可以通过使用解决最近 MSVC 中的 /Zc:preprocessor/experimental:preprocessor 选项.详情请参阅此处.

This issue might be resolved by using /Zc:preprocessor or /experimental:preprocessor option in recent MSVC. For the details, please see here.

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天全站免登陆