通过宏传递宏参数 [英] Passing macro arguments by macro

查看:127
本文介绍了通过宏传递宏参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将RGB颜色传递给带有三个参数的宏. 普通呼叫如下所示:MACRO(255, 255, 255) 宏会转换参数,并与运算符一起将其通过SPI发送到图形控制器.用C代码进行的调用如下所示:cmd(MACRO(0,0,0));

I'm trying to pass a RGB color into a macro that takes three arguments. A normal call would look like this: MACRO(255, 255, 255) The macro converts the arguments and together with an operator it is then sent over SPI to a graphics controller. A call in C code looks like this: cmd(MACRO(0,0,0));

由于颜色多次使用,但可能会发生变化,我认为通过另一个宏定义颜色可能是一个好主意. 因此定义颜色例如#define BLACK 0,0,0并将其插入另一个宏应该没问题:MACRO( BLACK )-> cmd(MACRO( BLACK )); 但是编译器会输出错误:

As the color is used on multiple occasions, but might be subject to change, i thought it might be a good idea to define it via another macro. So defining a color e.g. #define BLACK 0,0,0 and inserting it into the other macro should be ok: MACRO( BLACK ) -> cmd(MACRO( BLACK )); But the compiler outputs an error:

宏"MACRO"需要3个参数,但只给出1个

macro "MACRO" requires 3 arguments, but only 1 given

得到这个,我假设提供参数的宏没有被扩展.我试图找出通过gcc宏文档阅读时出了什么问题,但是无法根除它.我认为这是那些愚蠢的简单事情之一. 谢谢!

Getting this i assume the macro providing the arguments is not being expanded. I tried to figure out what was wrong reading through the gcc macro documentation, but was not able to sole it. I assume it is one of those stupid simple things... Thank you!

更多信息: 驱动程序提供了多个宏,它们以三个数字(rgb)作为参数.我想定义一种配色方案,以便每次出现时都可以更改颜色,而不会更改三个以上的数字.

More info: There are multiple macros provided by a driver taking three numbers (rgb) as an argument. I would like to define a color scheme to be able to change a color for every occurrence without changing more than those three numbers.

插入宏的函数占用的时间是无符号的,这就是宏所产生的.

The function where the macro is inserted is taking an unsigned long and that is what the macro is producing.

为什么术语BLACK不能简单地用0,0,0代替?

Why is the term BLACK not simply substituted by 0,0,0?

gcc宏文档中的语录:

Quote from the gcc macro documentation:

宏的所有参数在被替换为宏主体之前都已完全进行宏扩展.替换后,将再次扫描整个文本以查找要扩展的宏,包括参数."

"All arguments to a macro are completely macro-expanded before they are substituted into the macro body. After substitution, the complete text is scanned again for macros to expand, including the arguments."

推荐答案

您必须确保组合MACRO(BLACK)再次评估BLACK部分.否则,它不会看到"分隔零的逗号.您可以做的就是将现有的MACRO重命名为MACRO3(它会收到3个参数),然后具有

You'd have to ensure that your combination MACRO(BLACK) evaluates the BLACK part once more. Otherwise it doesn't "see" the commas separating the zeros. What you could to is rename your existing MACRO to MACRO3 (it receives 3 arguments) and then have

#define MACRO(...) MACRO3(__VA_ARGS__)

这篇关于通过宏传递宏参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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