r/C_Programming • u/jacksaccountonreddit • Dec 26 '22
Project Convenient Containers: A usability-oriented generic container library
https://github.com/JacksonAllan/CC
17
Upvotes
r/C_Programming • u/jacksaccountonreddit • Dec 26 '22
View all comments
Show parent comment
3
u/[deleted] Dec 29 '22
Thanks. There is some beauty in these hacks because underneath there is a regular C generic. Rather than having a separate compile time language e.g. "<int, struct foo>", your macros create a compile time layer on top of runtime generics. The compiler can emit optimized code with the constants provided by the macros, yet also there is an underlying runtime generic taking sizes and function pointers which could be utilized when type information isn't available at compile time.
I believe this is a better paradigm for generic compilation than the templating model of C++ because it's layered. The type system is made to lay on top of a regular generic function rather than be a prerequisite for it.
One might envision a new language which natively uses such a layered approach. Then one could link against a compiled generic library with the type info supplied at runtime, or build with library source and allow the compiler to emit optimized code. In the generic implementation one might write "sizeof(arg)" without care for whether that size is constant and without needing to explicitly pass size as a separate argument. "compare(a, b)" could also be automatically deduced too. Whether it's passed as a function pointer or inlined would be a decision for the build process, not the code. This orthogonality should better support code reuse, provide a more stable ABI, and reduce build times. I think there is great value in streamlining the hacks you have developed.