Alex Rivera | Logout

In C, Generic Containers or Safe Containers?

Asked 2012-04-29T05:37:28.783
10

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?

Edit
Report

1 Answer

0

you've no real benefit to using void* as a member (unless, say, compilation times are most important), you can always cast to void* where appropriate, then operate on untyped data blobs where necessary.

i've implemented such interfaces in C (e.g. multiple variants with named fields of different types). i favored having the type safety, and rarely found casting to void* a good thing in these implementations.

…then again, i spend more time writing c++ than c :)

answered 2012-04-29T05:53:57.463

Your Answer