有没有办法强制C预处理器在宏之前评估宏参数(Is there a way to force C preprocessor to evaluate macro arguments before the

编程入门 行业动态 更新时间:2024-10-25 16:29:25
没有办法强制C预处理器在宏之前评估宏参数(Is there a way to force C preprocessor to evaluate macro arguments before the macro)

我在表单中有许多宏

#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 B

and for readability I would like to define arguments for these macros e.g.

#define C A,B

so 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.

更多推荐

本文发布于:2023-08-07 05:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461276.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   参数   force   preprocessor   macro

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!