泛型在C11多参数的C函数 [英] Generics for multiparameter C functions in C11

查看:170
本文介绍了泛型在C11多参数的C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白了一个参数的函数C11仿制药,如:(从这里 >)

I understand C11 generics for one-parameter functions, like this: (from here)

#define acos(X) _Generic((X), \
    long double complex: cacosl, \
    double complex: cacos, \
    float complex: cacosf, \
    long double: acosl, \
    float: acosf, \
    default: acos \
    )(X)

但是,这似乎是功能的疼痛有两个参数,你需要嵌套调用 _Generic ,这是十分可怕的;来自同一博客摘录:

But, it seems to be a pain for functions with two arguments, you need to nest calls to _Generic, which is really ugly; Excerpt from the same blog:

#define pow(x, y) _Generic((x), \
long double complex: cpowl, \

double complex: _Generic((y), \
long double complex: cpowl, \
default: cpow), \

float complex: _Generic((y), \
long double complex: cpowl, \
double complex: cpow, \
default: cpowf), \

long double: _Generic((y), \
long double complex: cpowl, \
double complex: cpow, \
float complex: cpowf, \
default: powl), \

default: _Generic((y), \
long double complex: cpowl, \
double complex: cpow, \
float complex: cpowf, \
long double: powl, \
default: pow), \

float: _Generic((y), \
long double complex: cpowl, \
double complex: cpow, \
float complex: cpowf, \
long double: powl, \
float: powf, \
default: pow) \
)(x, y)

有没有办法来对多参数功能的更多人类可读的仿制药,这样,例如:

Is there a way to have more human being-readable generics for multiparameter functions, like this for instance :

#define plop(a,b) _Generic((a,b), \
      (int,long): plopii, \
      (double,short int): plopdd)(a,b)

在此先感谢您的答复。其基本思想是具有宏包装 _Generic

推荐答案

由于 _Generic 控股前pression不评估,我会建议申请某些算术运算,做适当的类型相结合,并在结果上进行切换。因此:

Given that the controlling expression of _Generic is not evaluated, I'd suggested applying some arithmetic operation that does the appropriate type-combining, and switching on the result. Thus:

#define OP(x, y) _Generic((x) + (y), \
    long double complex: LDC_OP(x, y), \
    double complex: DC_OP(x, y), \
    ... )

当然,这仅适用于某些情况下,但你总是可以扩展出那些其中的崩溃式是无益的。 (这将让一个以照顾阵列-N-OF-CHAR VS 的char * ,例如,与链接 printnl 例如,然后如果合并类型为 INT ,可以回去检查字符

Of course this only works for certain cases, but you can always expand out those for which the "collapsed" type is not helpful. (This would let one take care of array-N-of-char vs char *, for instance, as with the linked printnl example, and then if the combined type is int, one can go back and check for char and short.)

这篇关于泛型在C11多参数的C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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