我在表单中有许多宏
#define F(A,B) Some function of A and B为了便于阅读,我想为这些宏定义参数,例如
#define C A,B所以我可以说
F(C)但预处理器试图在C之前扩展F并抱怨F需要2个参数。 有没有办法让它在扩展F之前扩展C,以便不会发生错误?
I have a number of macros in the form
#define F(A,B) Some function of A and Band for readability I would like to define arguments for these macros e.g.
#define C A,Bso that I can say
F(C)but the preprocessor tries to expand F before C and complains that F needs 2 arguments. Is there a way to make it expand C before it expands F so that the error does not occur?
最满意答案
您可以使用带有可变数量参数的中间宏:
#define F1(A,B) #define F(...) F1(__VA_ARGS__) #define C A,B int main(void) { F(C) F(1,2) return 0; }这应该编译。 如果传递多于或少于两个参数,或者没有扩展到两个参数的参数,您仍会遇到编译失败。
You can use an intermediate macro that takes a variable number of arguments:
#define F1(A,B) #define F(...) F1(__VA_ARGS__) #define C A,B int main(void) { F(C) F(1,2) return 0; }This should compile. You will still get a compilation failure if you pass more or less than two arguments, or arguments that don't expand to exactly two arguments.
更多推荐
发布评论