This kind of vector bloat can only happen if you use vector<T*> for many different T types and if your STL implementation does not provide a wrapper around vector::<void*> for all these types (see the example in Stroustrup's book). I have never experienced such bloat with a decent compiler and STL implementation (MSVC and gcc).wgarvin wrote:Of course the downside of this approach is that you have to use the STL.Rein Halbersma wrote: There are many other places where explicit loops and comparisons could be eliminated by proper usage of the <algorithm> part of the STL.
Random example #1: STL container types are heavily templated, causing lots of generated code bloat in a large program that uses vector<> or other containers over lots of different types. I've seen large codebases that contained instantiations of vector<> for hundreds of different types, adding up to several megabytes of generated code. On some platforms you just cannot afford this. Even on PCs with abundant RAM and virtual memory, more code still means more page faults, or at least more icache misses. To avoid this penalty, we use our own container class implementations that are compatible with <algorithm> etc. but were designed to keep generated code size to a minimum while still being reasonably efficient.
Writing your own containers is no picknick either, and a source of many bugs if you are not very careful. I had written my own Array template class to store fixed-size move lists and it took a lot of effort to get it 100% right. Later I discovered that std::tr1::array provided the same functionality and I replaced my Array without any problems and eliminated a few 100 lines of code with it.
Code reduction and more abstract code is simply more maintainable and more bug-free than do-it-yourself containers and algorithms, and almost always more efficient as well.