For making that happen, he made use of the _Generic macro that C11 introducted for generic type selection. For those unfamiliar with it, I’ll begin with explaining the basics of the macro. For those of you interested in the linkedlist code, scroll down to the bottom of this article. You can always check out the C11 draft [section 188.8.131.52 Generic selection pg #96] (the standard costs around $60 but is essentially the same thing).
The _Generic macro follows the format
_Generic(controlling-expression, association-list) where controlling-expression is the expression whose type is to be detected, and association-list is the dictionary of type-expression pairs where type can be any object type and the expression can be any expression, constant or a pointer to any function, pretty much anything that can be evaluated.
To put it simply, compare it to the switch statement for type detection. For example,
The _Generic macro can be wrapped around in a nice looking function call, say detect_type(). Compare this to a (pseudo) switch statement which evaluates the case that matches the switch params
So that is it. A compile time type identification in C with a simple macro. We can do pretty amazing stuff with it using pointers to functions. For example, a generic print function, that can take in any type of argument and print it. I have added cases for integers, characters and string, but others can be added just as easily.
That was neat, wasn’t it. Now back to Aditya. He used the same macro to create a dynamically allocated linkedlist. I am pasting the gist here so that you can read his beautiful code.
To compile it, run
~$ gcc main.c linkedlist.c -o linkedlist on any machine with GCC installed (I’ve tested it on version >4.9). Run it with
~$ ./linkedlist and you should see the different elements that we’ve added printed on the standard out. You may also pack the linkedlist into a statically linked library, by running.
Interesting, isn’t it? Want to thank him, or have any comments or improvements? Drop them into the comment box. Thank you for reading.