如果你可以使用C11,_Generic可以帮助:
#includeint ifunc(int a, int b) { return a+b; } float ffunc(float a, float b) { return a+b; } double dfunc(double a, double b) { return a+b; } #define func(x, y) \ _Generic((x), int: ifunc, float: ffunc, double: dfunc, default: ifunc)(x, y) int main(void) { { int a = 1, b = 2, c; c = func(a, b); printf("%d\n", c); } { float a = .1f, b = .2f, c; c = func(a, b); printf("%f\n", c); } { double a = .1, b = .2, c; c = func(a, b); printf("%f\n", c); } return 0; }