Writing Sort-Protected Generics In C

Editorial Team
1 Min Read


The enjoyable half a couple of programming language like C is that though the language doesn’t instantly assist many options together with object-oriented programming and generics, there’s nothing that’s retaining you from implementing stated options in C. This extends to one thing like type-safe generics in C, as [Raph] demonstrates in a weblog put up.

After working by means of the assorted ways in which generics are additionally being applied utilizing strategies together with primary preprocessor macros and void pointers, the demonstrated methodology is launched. Whereas not essentially a brand new one, the benefit with this methodology is that’s type-safe. Very like C++ templates, these generics are evaluated at compile time, with the preprocessor dealing with each the kind checking and filling in of the fitting template snippets.

Whereas considerably verbose, it may be condensed right into a single header file, doesn’t depend on the void sort or pointers and could be deduplicated by the linker, stopping bloat. If generics is what you might be on the lookout for in your C undertaking, this may be a conceivable resolution.

Share This Article