5 ms·
You can do that since C11 using _Generic. https://en.cppreference.com/w/c/language/generic https://en.cppreference.com/w/c/language/generic: #include <math.h
by Someone 2y ago
You can do that since C11 using _Generic. https://en.cppreference.com/w/c/language/generic https://en.cppreference.com/w/c/language/generic:
#include <math.h>
#include <stdio.h>
// Possible implementation of the tgmath.h macro cbrt
#define cbrt(X) _Generic((X), \
long double: cbrtl, \
default: cbrt, \
float: cbrtf \
)(X)
int main(void)
{
double x = 8.0;
const float y = 3.375;
printf("cbrt(8.0) = %f\n", cbrt(x)); // selects the default cbrt
printf("cbrtf(3.375) = %f\n", cbrt(y)); // converts const float to float,
// then selects cbrtf
}
- strlenf 2y agoTrue but its very clumsy for large number of parameters. Even more importantly, its not forward compatible. For example, cbrt() cant reliably expanded to support bigints or any private types.