The short of it:
Yes, this approach will [probably] work in limited, specialized circumstances. I don't suspect std::vector
(or the rest of STL) to be among those circumstances.
The long of it:
As others have mentioned (and I agree), outside of an embedded system, code bloat isn't much of a problem on a compiled binary.
But, many of us suffer the compilation cost of building magnitudes more code during the compilation step than we might if there were compiled libraries to link against (instead of compiling header files). Add to it the difficulty of changing one of those templated header files and watch your entire project recompile from scratch. Long compile times make for sad developers :(
It may not affect a large percent of developers - depending on the size of your company's codebase, and how you structure your build environment. It certainly taxes us at my company.
As some answers have pointed out, there isn't much to abstract from a std::vector
that would make it fair any better in your example. Certainly you need to be able to create and destroy individual elements, and making any methods virtual
would hinder run-time performance (which is more important than compile-time performance). In addition, the compiler will lose the ability to optimize the void*
library for templated code, this could result in a large performance loss.
I'm more interested in the more complex structures - would std::map
benefit from abstraction? What if you took the red-black tree (the SGI implementation) out and linked against a red-black tree library. You would (probably) need to store the elements outside of the std::map
, so it doesn't need to call the destructors, but that might (again) cause a run-time performance loss due to doubling your indirection.
I'm fairly certain that you couldn't use this method to improve on STL's implementation.
If you had better knowledge of the data structures you were storing, or you had a very specific templated type (not necessarily a container), you could probably improve your performance. But, then the question becomes - how often will you use this new templated type such that the overhead of compiling it will be noticeably improved? Certainly it would help compile times for std::vector
, but maybe not for my_domain_specific_ptr_container
. If you save 50% of compile time for my_domain_specific_ptr_container
, how many times would you have to use it to notice a significant enough build boost to justify the added complexity to the class (and reduced debug ability).
If you haven't already, your time may be better spent distributing your build tools :)
If, on the other hand, you try this and it works for STL containers... please post back. I want a faster build! ;)