In C++ you can have both generic and type safe containers by using templates. However in C, if you want generic containers, you have to (afaik) use void*, which means you lose type safety. To have type safe containers, you would have to reimplement them for every type of data you want to hold.
Given that C follows a more the-programmer-knows-what-he’s-doing philosophy than C++, what would be the more idiomatic thing to do in C: use generic containers with void*, or make custom containers for every type of data?
I’d aim for generic containers:
Once you get used to it, you just think of
void *is meaning the type of something when I don’t care about it’s type. It’s likeObjectin Java — where, for a long time, generic containers didn’t have type safety either.You only have one place to make improvements.
You don’t get the type safety; but with repeated implementations of type safe containers, you run the risk of copy and paste errors. That can lead to errors, too.