Generics for multiparameter C functions in C11

前端 未结 5 1353
独厮守ぢ
独厮守ぢ 2020-12-13 07:13

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

#define acos(X) _Generic((X), \\
    long double complex: cacosl, \\
    double         


        
5条回答
  •  生来不讨喜
    2020-12-13 07:50

    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), \
        ... )
    

    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.)

提交回复
热议问题