如何强制处理器在粘贴前使用表达式结果

时间:2022-11-25 10:48:47

My code is

我的代码是

#define PASTE__(a, b) a##b
#define PASTE_(a, b)  PASTE__(a, b)
#define PASTE(a, b) PASTE_(a, b)

int main()
{
    PASTE(1, (1+3)/4);
    return 0;
}

I would LIKE to have the result be

我希望得到结果。

int main()
{
    11;
    return 0;
}

Compilable link: http://coliru.stacked-crooked.com/a/b35ea3e35a1b56ae

Compilable链接:http://coliru.stacked-crooked.com/a/b35ea3e35a1b56ae

I put in two levels of indirection suggested by How can I guarantee full macro expansion of a parameter before paste?.

我提出了两个层次的间接建议,如何保证一个参数在粘贴前的宏展开?

But still I get a preprocessor error:

但还是有一个预处理器错误:

main.c:8:11: error: pasting "1" and "(" does not give a valid preprocessing token
     PASTE(1, (1+3)/4);
           ^
main.c:1:23: note: in definition of macro 'PASTE__'
 #define PASTE__(a, b) a##b
                       ^
main.c:3:21: note: in expansion of macro 'PASTE_'
 #define PASTE(a, b) PASTE_(a, b)
                     ^
main.c:8:5: note: in expansion of macro 'PASTE'
     PASTE(1, (1+3)/4);

How do I get the preprocessor to resolve the result of that expression before doing concatenation?

如何让预处理器在进行连接之前解析表达式的结果?

2 个解决方案

#1


3  

It looks like you're trying to get the preprocessor to evaluate some simple mathematical operations and convert to the result. This is not possible without substantial extra macro infrastructure to perform the necessary math. The easiest way to get the needed infrastructure is probably to use BOOST_PP.

看起来您正在尝试让预处理器评估一些简单的数学操作并将其转换为结果。如果没有大量额外的宏观基础设施来执行必要的计算,这是不可能的。获得所需基础设施的最简单方法可能是使用BOOST_PP。

http://www.boost.org/doc/libs/1_59_0/libs/preprocessor/doc/index.html

http://www.boost.org/doc/libs/1_59_0/libs/preprocessor/doc/index.html

You would need to modify your code so that macros are used to perform the math rather than operators. The line in question would look like:

您需要修改代码,以便使用宏执行数学运算,而不是操作符。这句话应该是:

PASTE(1, BOOST_PP_DIV(BOOST_PP_ADD(1,3),4));

Now the answer would come out as 11, and I assume that's what you're looking for.

答案是11,我猜这就是你要找的。

#2


0  

What exactly are you hoping the preprocessor output for the PASTE line will be? 11;? That's impossible. (update: apparently it is possible with clever enough macros. See Charles Ofria's answer).

您希望粘贴行的预处理器输出是什么?11;?这是不可能的。(更新:显然,有足够聪明的宏是可能的。见查尔斯Ofria的回答)。

That double-expansion with multiple macros is only useful for expanding macros and then stringifying the results, IIRC.

使用多个宏进行双展开只对扩展宏有用,然后对结果进行字符串化,IIRC。

#1


3  

It looks like you're trying to get the preprocessor to evaluate some simple mathematical operations and convert to the result. This is not possible without substantial extra macro infrastructure to perform the necessary math. The easiest way to get the needed infrastructure is probably to use BOOST_PP.

看起来您正在尝试让预处理器评估一些简单的数学操作并将其转换为结果。如果没有大量额外的宏观基础设施来执行必要的计算,这是不可能的。获得所需基础设施的最简单方法可能是使用BOOST_PP。

http://www.boost.org/doc/libs/1_59_0/libs/preprocessor/doc/index.html

http://www.boost.org/doc/libs/1_59_0/libs/preprocessor/doc/index.html

You would need to modify your code so that macros are used to perform the math rather than operators. The line in question would look like:

您需要修改代码,以便使用宏执行数学运算,而不是操作符。这句话应该是:

PASTE(1, BOOST_PP_DIV(BOOST_PP_ADD(1,3),4));

Now the answer would come out as 11, and I assume that's what you're looking for.

答案是11,我猜这就是你要找的。

#2


0  

What exactly are you hoping the preprocessor output for the PASTE line will be? 11;? That's impossible. (update: apparently it is possible with clever enough macros. See Charles Ofria's answer).

您希望粘贴行的预处理器输出是什么?11;?这是不可能的。(更新:显然,有足够聪明的宏是可能的。见查尔斯Ofria的回答)。

That double-expansion with multiple macros is only useful for expanding macros and then stringifying the results, IIRC.

使用多个宏进行双展开只对扩展宏有用,然后对结果进行字符串化,IIRC。