0%

MSVC and __VA_ARGS__

今天才知道這個Bug

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

gcc和clang的結果都是

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

不過MSVC的結果是

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

把以前的bug當feature了
修正方法有兩個
一個是在編譯的時候加上/Zc:preprocessor,不過CMake project預設就開了
另一個是加上另外一層Macro

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