r/C_Programming • u/jacksaccountonreddit • Dec 26 '22
Project Convenient Containers: A usability-oriented generic container library
https://github.com/JacksonAllan/CC
16
Upvotes
r/C_Programming • u/jacksaccountonreddit • Dec 26 '22
View all comments
Show parent comment
4
u/jacksaccountonreddit Dec 28 '22 edited Dec 30 '22
User-extendable generic macros
A major limitation of C’s
_Genericstatement is that all the accepted types must be known by the person writing it. So a library writer cannot create a_Genericmacro with which the library users can easily plug in their own types – or so it seems.In fact, we can circumvent this limitation by providing empty “slots” to be filled in by user-defined types in our
_Genericstatement. I previously described the mechanism here and here. Here’s a simple example of a user-extendible generic hash macro:CC uses this mechanism for user-defined destructor, comparison, and hash functions and hash table load factors, as well as for deducing map key types. At each API macro invocation, extendible generic macros deduce these functions from the container pointer and pass them, along with the pointer, into relevant internal library function. But CC’s extendibility system is more sophisticated than the example above because it also uses a preprocessor counter system to allow up to 511 types without gigantic blocks of code.