Re:Type-erased generic functions for C: A modest non-proposal
by strlenf on Thursday October 03, 2024 @06:55AM
(#41729394)
Here is my not-very-novel proposal for function overriding only. Its backward/forward compatible and free of name-mangling.
[ Parent
| on HN ]
Re:Type-erased generic functions for C: A modest non-proposal
by Someone on Thursday October 03, 2024 @07:22AM
(#41729557)
You can do that since C11 using _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
}
Re:Type-erased generic functions for C: A modest non-proposal
by strlenf on Thursday October 03, 2024 @08:07AM
(#41729914)
True 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.