小编amc*_*lde的帖子

使用C宏调用关联*函数

*不要与与关联阵列有任何关系混淆.

我知道如何使用宏来向量化C中的函数,以提供类似于Mathematica的Map(或Apply)功能的结果.即将函数应用于参数列表.

#define Apply( type, function, ...)             \
{                                               \
   void *Stop = (int[]){0};                     \
   type **List = (type*[]){__VA_ARGS__, Stop};  \
   for( int i = 0; List[i] != Stop; ++i )       \
   function( List[i] );                         \
}                     
Run Code Online (Sandbox Code Playgroud)

然后我可以做类似的事情

#define FreeAllAtOnce(...) Apply( void, free, __VA_ARGS__ );
Run Code Online (Sandbox Code Playgroud)

这有效果

free( Array1 );
free( Array2 );
free( Array3 );
Run Code Online (Sandbox Code Playgroud)

相当于

FreeAllAtOnce( Array1, Array2, Array3 );
Run Code Online (Sandbox Code Playgroud)

我没有说明这一点,我在一本书中读到了它并且从那以后大量使用它.

我的问题是:我可以做类似的事情通过一些二进制函数关联组合一个数组.例如,采用GCD功能.我想要一个像这样的功能:

GCD_all( a, b, c, d, e );
Run Code Online (Sandbox Code Playgroud)

这与效果相同

GCD( GCD( GCD( GCD( a, b ), c …
Run Code Online (Sandbox Code Playgroud)

c macros c-preprocessor

8
推荐指数
1
解决办法
307
查看次数

标签 统计

c ×1

c-preprocessor ×1

macros ×1