替代 C 中的函数重载?

暗库

由于在 C 中不允许直接进行函数重载,那么如何为这些函数进行重载呢?

void MotorDriver_Create(float speedAddress,float frequencyAddress, float db_input);
void MotorDriver_Create(float speedAddress, float frequencyAddress, float minDB, float maxDB);
戈文·帕尔玛

没有办法实现类似于 C++ 中可用的函数重载,因为与 C++ 不同,函数签名不会以允许重载的方式进行修改。可以做的是使用_Generic关键字将宏映射到最适合参数类型的函数调用中。例如:

#include <math.h>
#include <float.h>
#define sqrt(X) _Generic((X), \
            long double: sqrtl, \
            default: sqrt, \
            float: sqrtf \
)(X)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章