当扩展最外面的宏时,是否有一种方法可以将宏名称作为参数传递给嵌套宏,而无需扩展宏呢? [英] Is there a way of passing macro names as arguments to nested macros without them being expanded when the outermost macro is expanded?

查看:78
本文介绍了当扩展最外面的宏时,是否有一种方法可以将宏名称作为参数传递给嵌套宏,而无需扩展宏呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(道歉长标题,但我不能认为较不具体的一个这将是清楚的足够)

(Apologies for the long title, but I couldn't think of a less specific one which would be clear enough)

我需要一个(对象等)宏的名称传递给一个嵌套(函数样)的宏,如下面的(简单)的例子:

I need to pass the name of an (object-like) macro to a nested (function-like) macro, as in the following (trivial) example:

#define ROOT_FUNC(INPUT) int v_ ## INPUT = INPUT
#define CALLER_FUNC(INPUT) ROOT_FUNC(INPUT)

#define INTA 1
#define INTB 2
#define INTC 3

现在,如果我在代码中写入ROOT_FUNC(INTA);,我将得到一个名为v_INTA的整数变量,其值为1.如果我在代码中定义的变量,,然后写我结束了称为与值

Now, if I write ROOT_FUNC(INTA); in my code I get an integer variable called v_INTA with the value 1. If I define a variable in code, int INTD = 4;, and then write CALLER_FUNC(INTD); I end up with an integer variable called v_INTD with the value 4.

但是如果我写CALLER_FUNC(INTA);,我会得到一个名为v_1的整数变量,其值是1,因为INTACALLER_FUNC展开时(在ROOT_FUNC之前)被扩展为1.被扩展(即ROOT_FUNC(1)被扩展).

But if I write CALLER_FUNC(INTA); I get an integer variable called v_1 with a value of 1, because INTA is expanded to 1 at the time CALLER_FUNC is expanded, before ROOT_FUNC is expanded (i.e. ROOT_FUNC(1) is what gets expanded).

如果我变化管线2:(即字符串化)时,发生一个编译器错误,因为它被要求来定义称为(无效的名称)的整数变量,并给它的值"1"(非整数值).

If I change line 2 to: #define CALLER_FUNC(INPUT) ROOT_FUNC(#INPUT) (i.e. stringifying INPUT), a compiler error occurs because it is being asked to define an integer variable called v_"1" (an invalid name) and give it the value "1" (a non-integer value).

我知道预处理器是相当原始,但有获得我后的任何方式?

I know the preprocessor is fairly primitive, but is there any way of achieving what I'm after?

(为进一步说明,进行第二次编辑,我想先将CALLER_FUNC(INTA);扩展到ROOT_FUNC(INTA);,然后再扩展到int v_INTA = 1; –即我希望将INTA扩展到ROOT_FUNC内部,而不是外部.我是寻找在原则答案,不是任何的方式来改变以产生结果,这将是微不足道的).

(Second edit for further clarification, I want CALLER_FUNC(INTA); to expand first to ROOT_FUNC(INTA);, then to int v_INTA = 1; – i.e. I want INTA to be expanded inside ROOT_FUNC, rather than outside it. I am looking for an answer in principle, not just any way to change CALLER_FUNC to produce the result int v_INTA = 1;, which would be trivial).

P.S. 如果您想知道,我最初有一个涉及信号处理的用例(例如,将SIGINT之类的宏名称用作嵌套宏的输入),但是通过简化我的结构并放弃了嵌套宏来克服了这些限制;因此该问题纯属学术.

P.S. In case you are wondering, I originally had a use case involving signal handling (e.g. taking macro names like SIGINT as inputs for nested macros), but got around these limitations by simplifying my structure and abandoning nested macros; hence this question is purely academic.

推荐答案

如果您可以展开第一个宏取两个参数,你可以得到它像这样工作:

If you can expand the first macro to take two arguments, you could get it to work like this:

#define FUNC(intname, intv) int v##intname = intv
#define CALL_FUNC(intv) FUNC(_##intv, intv)

#define INT1 1
#define INT2 2

int main(void)
{
  int INTD = 4;

  CALL_FUNC(INT1);
  CALL_FUNC(INT2);
  CALL_FUNC(INTD);
}

(来自GCC的)输出看起来像这样:

The output (from GCC), looks something like this:

int main(void)
{
  int INTD = 4;

  int v_INT1 = 1;
  int v_INT2 = 2;
  int v_INTD = INTD; // not sure if you want the value of INTD here - I guess it doesn't matter?
}

我的猜测是你所追求的 - 如果我看了你的问题的权利.

Which I guess is what you are after - if I read your question right?

在标记粘贴防止从膨胀出来,并简单地将预处理器产生被传递给第二个宏(其然后简单地粘贴该在一起以形成可变的)一个新的令牌,该值(其被展开)向下传递作为第二个参数.

The token pasting prevents the preprocessor from expanding it out and simply generates a new token which is passed to the second macro (which then simply pastes that together to form the variable), the value (which is expanded) is passed down as the second argument..

EDIT1 :请仔细阅读您所追求的内容,我想以上技巧并不是您真正想要的...嗯.

EDIT1: Reading more through what you are after, I'm guessing the above trick is not what you reall want...ah well..

这篇关于当扩展最外面的宏时,是否有一种方法可以将宏名称作为参数传递给嵌套宏,而无需扩展宏呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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