I
Ian Collins
It's quite difficult to kludge any kind of generic data containers in C
(which is probably the reason why the standard C library does not offer
any such containers). You could achieve something similar with preprocessor
macros, but it would be quite limited, and definitely less safe (in big
part because of the lack of RAII, constructors, destructors, etc).
I think you are spot on with why C lacks standard containers. But it
does have a limited set of generic algorithms.
You could perhaps make some more or less generic containers for basic
types and structs which only contain basic types (but not pointers) with
preprocessor macros, but immediately when you need more resource management
than that (such as a struct that as pointers pointing to dynamically
allocated memory or other such resources) it becomes complicated. For that
same reason it's complicated to make the containers nested. (Assigning
a container object would not be the same as assigning a basic type.)
If you want an idea how complicated it is, have a look at the C
containers library Jacob Navia has been developing
(http://code.google.com/p/ccl/).
It's so much simpler with C++ templates. Just write any amount of
nested containers you want, no problem.
std::deque<std::list<std::vector<std::set<std::string> > > > container;
I'm not saying it's not a lot easier to do generic containers or
algorithms with templates. I'm saying it's impossible to do RAII in C.
I've often been asked by teams I've managed or coached why they should
look at using C++ rather than C and my response normally something along
the lines of "what's an automatic, simple means of preventing resource
leaks worth to you?" Then we get onto the other stuff! One (DSP) team
was happy just to use C++ as "C with RAII (and function overloading)".