简体   繁体   English

(C99)在不同的宏中展开宏

[英](C99)Expand a macro in a different macro

I have a function in my program that takes 3 arguments. 我的程序中有一个函数,它有3个参数。 Some times in the code there is a macro defining 2 of these parameters. 有时在代码中有一个宏定义了这些参数中的2个。

So this: 所以这:

void func(int x, int y, int z){...}

Can be invoked like this: 可以像这样调用:

#define PAR 10,20
int z = 3;
func(PAR, z);

Now, I need to change my code so that the function is called like a macro for another function. 现在,我需要更改我的代码,以便调用该函数,就像另一个函数的宏一样。

#define func(X,Y,Z) func2(X,Y,Z,#Z)

This works fine if X and Y are really passed as variables. 如果X和Y真的作为变量传递,这可以正常工作。 Is there any way to make it work also with the macro PAR? 有没有办法让它与宏PAR一起工作?

I'm using GCC 4.6 我正在使用GCC 4.6

You can do this with an extra level of indirection, (ab)using variadic macros: 您可以使用额外的间接级别(ab)使用可变参数宏来执行此操作:

#include <stdio.h>
#define PAR 2,3
#define F(...) G(__VA_ARGS__)
#define G(a,b,c) H(a,b,c)
void H(int a, int b, int c) {
  printf("%d %d %d\n", a , b, c);
}
int main() {
  F(PAR, 42);
  return 0;
}

There is probably a better solution for the underlying problem. 对于潜在的问题,可能有更好的解决方案。

No, I don't believe so. 不,我不相信。 When you define 当你定义

#define func(X,Y,Z) func2(X,Y,Z,#Z)

You're defining a function-like macro . 你正在定义一个类似函数的宏 func(X,Y,Z) actually takes three arguments - and it has to take three arguments . func(X,Y,Z)实际上有三个参数 - 它必须带三个参数 Remember, the preprocessor and not the compiler is interpreting func(PAR, Z) . 请记住, 预处理器不是编译器正在解释func(PAR,Z)

I've struggled to find any documentation, but it makes sense that the first thing the preprocessor will do (considering that func() is the outer element) is to check to see if the arguments to func() are valid. 我一直在努力寻找任何文档,但有意义的是,预处理器将做的第一件事(考虑到func()是外部元素)是检查func()的参数是否有效。 Then it will place the arguments into func2() and will then expand any macros that were passed as arguments. 然后它将参数放入func2() ,然后展开作为参数传递的任何宏。 The code I placed below seems to back up this claim. 我下面的代码似乎支持了这一说法。

Following this logic, the preprocessor will see that func(PAR, Z) isn't a valid call because an argument is missing, which will then throw the error 遵循这个逻辑,预处理器将看到func(PAR,Z)不是一个有效的调用,因为缺少一个参数,然后会抛出错误

13:12: error: macro "func" requires 3 arguments, but only 2 given

func(X, Y, Z) will work so long as X and Y are valid macros or variables. 只要XY是有效的宏或变量,func(X,Y,Z)就可以工作。

Code ( this will give you warnings because there is no function declaration , but the output will be " 3 14 3 " as expected): 代码( 这会给你警告,因为没有函数声明 ,但输出将是“ 3 14 3 ”,如预期的那样):

#include <stdio.h>
#include <stdlib.h>

#define PAR 10,20
#define MAR 3
#define WAR 14
#define func(X,Y,Z) print(X, Y, Z)

int Z = 3;

int main(void){

        func(MAR,WAR,Z);

        return 0;
}

void print(int x, int y, int c){

        printf("%d %d %d\n", x, y, c);

}

Out of curiosity, why are you doing this (I don't have enough reputation to comment yet FYI). 出于好奇,你为什么这样做(我没有足够的声誉来评论FYI)。

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

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