Trick to overload C functions

June 19, 2013
C

Here’s a little known trick, clang supports overloading of C functions using the overloadable attribute.

As an example, you could overload a function to compute the sine with a float, double, or long double:

float __attribute__((overloadable)) sin(float x) { return sinf(x); }
double __attribute__((overloadable)) sin(double x) { return sin(x); }
long double __attribute__((overloadable)) sin(long double x) { return sinl(x); }

You can read more about C function overloading in the clang language extensions documentation.