c - 替代 C 中的函数重载?

标签 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)

关于c - 替代 C 中的函数重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54410894/

相关文章:

c - Linux 内核 - 读/写文件

c - 为什么 main 函数会出现 "control reaches end of non-void function"警告?

C:分配中的不兼容类型

c - printf ("%p"int 1) 是什么意思?

c - 通过 C 中的管道使用动态数组写入和读取结构

c - C中的字符串操作

c++ - C++ 中字符串和 wstring 的控制台打印

c - C 编程中的输入验证

c - 更多的返回声明与更多的缩进

c - 为什么 void 指针的大小在 Windows 64 位平台上是 4