2 ms·
#define add(a, b) _Generic(a, int: addi, char*: adds)(a, b) Just because you can doesn't mean you should. Someday, someone will have to read that code. Note
by Animats 11y ago
#define add(a, b) _Generic(a, int: addi, char*: adds)(a, b)
Just because you can doesn't mean you should. Someday, someone will have to read that code. Note that when "add" returns a string, it's done an allocation, but for the integer case, it hasn't called "malloc". Now you'll now need to know the type to get rid of it.
Back in C99, generic trig functions were put in to make the number-crunching people happy. After all, FORTRAN has them. Everybody was tired of different versions of math libraries for "float" and "double". Then in C11, the mechanism behind that, "_Generic", was exposed. Now, anybody can use it. But, probably, they shouldn't. [1]
[1] http://www.robertgamble.net/2012/01/c11-generic-selections.html http://www.robertgamble.net/2012/01/c11-generic-selections.h...
- maxlybbert 11y ago> Just because you can doesn't mean you should. The fact that it's new doesn't mean it's in bad taste. GCC has had a similar mechanism in C for decades. It was experience with that mechanism, and similar extensions in other compilers, that led to it being in the C Standard.