r/C_Programming • u/Object_71 • Aug 25 '26
Writing generic code in C – Part 2
https://thatonegamedev.com/cpp/writing-generic-code-in-c-part-2/After some comments on my previous post about writing generic code in C where people argue that this is “poor man’s overloading” I wanted to add a new technique that allows you to write real generic style code in C with the only drawback. You could even combine the technique from this lesson and the […]
32
Upvotes
19
u/Snarwin Aug 25 '26
The problem with using concatenation like this is that it falls apart as soon as you want to use a type like
const char *that contains non-identifier characters. It's possible to work around this with atypedef, but really the correct approach is to let the user#definebothTandTNameseparately.30
u/SmokeMuch7356 Aug 25 '26
Hot take incoming...
Generic programming in C is a fool's errand. Yes, you can do it, I've done it. It's a shitload of work, it's easy to get wrong, it will never be 100% type safe, it requires awkward programming styles (the alternating
#defines and#includes make me itch), on and on and on and on._Generichelps, but not that much.For my part I take cues from
qsortandbsearch- treat everything as avoid *, use type-aware callbacks for comparisons, assignment, output or formatting, etc. Unfortunately that throws type safety out the window and into oncoming traffic (and you still have to create type-aware front ends if you want to be able to handle literal values). IMO it's miles less confusing than macro-based approaches, but it's still a half-assed solution.There's just no good way to do it. It's more of a pain in the ass than it's worth. If you absolutely need true generic support, use C++, or Java, or a language with the capability built into it. C at best gives you a crayon drawing that can look a little like generic support.
4
u/pjl1967 Aug 25 '26
Many make the mistake of trying to put the type into the container using macros for the container itself. Among other things, that way leads to code bloat. There is another way as described here. (You can scroll down to Adding Type Information.)
2
u/massive_cerebrum Aug 31 '26
A link to part 1 would be nice
1
u/Physical_Dare8553 Aug 25 '26
string concatonation is not the way for this because of complex types, like const pointers and such. i think it's better to let the user provide a name and a type separatley
i dont use this method for arrays but i have something similar for my hashmaps
`
#define mapconfig dbgallocator_map, void *, struct tracedata, ((iptr)k), ((iptr)a - (iptr)b)
#include "../incmap.h"
`
1
u/theNbomr Aug 27 '26
I cannot envision producing by hand, code that reads like what you're describing, ever.
However... I can easily imagine some kind of C source code generator that is the back end of some kind of tool leveraging it. I'm thinking of GUI producing tools or perhaps code to produce an implementation of network protocols a la ASN.1
There is a good bit of merit to your project in getting a lively discussion going between people who have some advanced knowledge and skills.