简体   繁体   English

将宏参数传递给带逗号的函数

[英]Passing macro arguments to function with comma

In the MSP430 world, the driverlib has functions for working with GPIO pins: 在MSP430世界中,driverlib具有使用GPIO引脚的功能:

GPIO_SetAsInputPin(port, pin)

I would like a single macro to define a single pin, for example: 我想要一个宏来定义一个引脚,例如:

#define PB1     (GPIO_PORT_P2, GPIO_PIN4)
#define PB2     (GPIO_PORT_P2, GPIO_PIN5)

When I call the function with the macro: 当我使用宏调用该函数时:

GPIO_setAsInputPin(PB1);

I get a too few arguments in function call error. 我在函数调用错误中得到的参数太少。 Is there a different way of making this work? 有其他方法可以使这项工作吗?

The parentheses around the expansion of PB1 make it into a single argument with a comma operator in between — so your invocation expands to: PB1扩展的括号使它成为单个参数,并且在两者之间使用逗号运算符,因此您的调用扩展为:

GPIO_setAsInputPin((GPIO_PORT_P2, GPIO_PIN4))

You should probably use: 您可能应该使用:

#define PB1   (GPIO_PORT_P2), (GPIO_PIN4)

which expands to: 扩展为:

GPIO_setAsInputPin((GPIO_PORT_P2), (GPIO_PIN4))

giving you two arguments. 给你两个论点。 Or you could omit the parentheses around the names and you'd probably be safe. 或者,您可以省略名称周围的括号,这样可能很安全。

You may still have problems if GPIO_setAsInputPin is itself a macro that expects two arguments. 如果GPIO_setAsInputPin本身是一个需要两个参数的宏,您可能仍然会遇到问题。 Then you'd need something like: 然后,您将需要以下内容:

#define GPIO_setAsInputPin_1(x)    GPIO_setAsInputPin(x)

and you would invoke: 然后您将调用:

GPIO_setAsInputPin_1(PB1);

and you'd get as output: 然后您将得到输出:

GPIO_setAsInputPin((GPIO_PORT_P2), (GPIO_PIN4));

or whatever the underlying macro expands to. 或底层宏扩展到的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM