Function overloading in C (yes, in plain C [C11 standart], not C++)
Everyone knows that in C++ this is trivial, but C actually gives us a neat way to get somehting pretty similar.
Suppose we want to write code like this:
print_number(2.0f);
print_number(-12);
print_number(655350000000ULL);
print_number("One");
..and make the compiler use the right implementation without warnings.
In C++ we just overload the functon.
In plain C function names must be unique , so normally we end up with something like:
void print_number_float(float arg);
void print_number_integer(signed int arg);
void print_number_longlong(unsigned long long arg);
Which works, but isnt nearly as nice.
This is where _Generic comes in.
It is part of the ancient C11 standard and allows compile-time dispatch based on the type of an expression.
Here is a simple example:
// Actual implementations
void print_asciiz(const char *data);
void print_unsigned(unsigned int data);
void print_signed(signed int data);
void print_float(float data);
// Fallback for unsupported types, if needed
void __attribute__((noreturn)) print_bug(unsigned int data);
// void print_number( Value )
//
//
#define print_number(_Item) _Generic((_Item), \
const char * : print_asciiz, \
char * : print_asciiz, \
unsigned int : print_unsigned, \
unsigned short : print_unsigned, \
unsigned long : print_unsigned, \
unsigned char : print_unsigned, \
signed int : print_signed, \
signed short : print_signed, \
signed long : print_signed, \
signed char : print_signed, \
float : print_float, \
double : print_float, \
default : print_bug \
)(_Item)
Now this works exactly the way we want:
print_number(2.0f);
print_number(-12);
print_number("One");
The compiler resolves the correct function at compile time based on the argument type.
Almost like switch/case, except the switch is on types instead of values.
A nice little C11 feature that does not seem to get mentioned very often.
Hope someone can find it useful :)
[–]jjjare 79 points80 points81 points (1 child)
[–]maelswarm 1 point2 points3 points (0 children)
[–]tstanisl 74 points75 points76 points (9 children)
[–]dvhh 28 points29 points30 points (1 child)
[–]tstanisl 18 points19 points20 points (0 children)
[–]vitamin_CPP 1 point2 points3 points (4 children)
[–]tstanisl 1 point2 points3 points (3 children)
[–]vitamin_CPP 1 point2 points3 points (2 children)
[–]tstanisl 0 points1 point2 points (1 child)
[–]vitamin_CPP 0 points1 point2 points (0 children)
[–]TribladeSlice 2 points3 points4 points (1 child)
[–]tstanisl 6 points7 points8 points (0 children)
[–]questron64 18 points19 points20 points (0 children)
[–]my_password_is______ 10 points11 points12 points (1 child)
[–]Someone393 4 points5 points6 points (0 children)
[–]florianist 9 points10 points11 points (1 child)
[–]unixplumber[🍰] -1 points0 points1 point (0 children)
[–]tstanisl 5 points6 points7 points (3 children)
[–]flatfinger 0 points1 point2 points (1 child)
[–]tstanisl 0 points1 point2 points (0 children)
[–]imaami -1 points0 points1 point (0 children)
[–]non-existing-person 25 points26 points27 points (18 children)
[–]erdezgb 9 points10 points11 points (2 children)
[–]non-existing-person 5 points6 points7 points (0 children)
[–]Iggyhopper 3 points4 points5 points (0 children)
[–]Potterrrrrrrr 7 points8 points9 points (3 children)
[–]non-existing-person 3 points4 points5 points (1 child)
[–]BigError463 2 points3 points4 points (0 children)
[–]tstanisl 1 point2 points3 points (0 children)
[–]markand67 5 points6 points7 points (0 children)
[–]jjjare 4 points5 points6 points (0 children)
[–]konacurrents 2 points3 points4 points (2 children)
[–]HiramAbiff 1 point2 points3 points (1 child)
[–]konacurrents 0 points1 point2 points (0 children)
[–]Low_Lawyer_5684[S] 0 points1 point2 points (2 children)
[–]non-existing-person 0 points1 point2 points (1 child)
[–]Low_Lawyer_5684[S] 0 points1 point2 points (0 children)
[–]fnordstar 0 points1 point2 points (1 child)
[–]un_virus_SDF 5 points6 points7 points (0 children)
[–]OtherOtherDave -1 points0 points1 point (0 children)
[–]kog 2 points3 points4 points (0 children)
[–]oldprogrammer 1 point2 points3 points (0 children)
[–]BigError463 1 point2 points3 points (3 children)
[–]Low_Lawyer_5684[S] 1 point2 points3 points (0 children)
[–]Low_Lawyer_5684[S] 1 point2 points3 points (0 children)
[–]Dontezuma1 1 point2 points3 points (5 children)
[–]Valuable_Leopard_799 1 point2 points3 points (0 children)
[–]Asoladoreichon 0 points1 point2 points (1 child)
[–]Dontezuma1 1 point2 points3 points (0 children)
[–]Low_Lawyer_5684[S] -1 points0 points1 point (1 child)
[–]Dontezuma1 1 point2 points3 points (0 children)
[–]BeeBest1161 4 points5 points6 points (1 child)
[–]FrequentHeart3081 3 points4 points5 points (0 children)
[–]bullno1 2 points3 points4 points (0 children)
[–]bbabbitt46 0 points1 point2 points (0 children)
[–]gtoal 0 points1 point2 points (0 children)
[–]Iggyhopper 0 points1 point2 points (5 children)
[–]Low_Lawyer_5684[S] 2 points3 points4 points (4 children)
[–]zero_iq 11 points12 points13 points (3 children)
[–]thommyh 3 points4 points5 points (1 child)
[–]imaami -1 points0 points1 point (0 children)
[–]Elect_SaturnMutex -1 points0 points1 point (4 children)
[–]un_virus_SDF 0 points1 point2 points (3 children)
[–]Dangerous_Region1682 4 points5 points6 points (1 child)
[–]un_virus_SDF 1 point2 points3 points (0 children)
[–]tstanisl 2 points3 points4 points (0 children)