TITLE: What good are templates, anyway? RESPONSE: rmartin@rcmcon.com (Robert Martin) Recently a friend of told me of an article in the Microsoft Systems Journal regarding the new release of Visual C++. He said he thought I would be interested in some of the comments made by the author with regard to templates. The author said that the next release of VC++ will have templates; even though there was no standard for templates yet... This struck me as being funny. There is no standard for C++ yet, so why should he make an issue out of templates? He went on to say that he thought the feature would not be used since MFC already supplies container classes without templates. To quote verbatim: "Now maybe I'm missing the big picture, but every article I've read about class templates says the same thing -- they make it easier to implement collection class libraries. But since MFC already provides collection classes, I don't expect programmers working in Visual C++ to write their own just so they can experience the joys of C++ class templates. That's how I see it." I agree with him, maybe he has missed the big picture. Templates are not simply a special facility to make collection classes easier. They are a programming tool that allows type safety to be integrated with generic algorithms. For example, I would expect (hope) that the users of VC++ would begin to wrap the old collection classes with template interfaces so that they could have type safe collections. Perhaps this is already under weigh since the author did say that VC++ would supply some template collection classes. However, beyond simple containers, template classes are useful for any set of algorithms and data structures that can be generically specified for a number of unrelated types. They implement a kind of static, name based polymorphism. For example, consider that we have an abstract base class which supplies an interface for all clients of a GUI button: class ButtonClient { public: virtual void ButtonPressed() const = 0; }; The GUI maintains a list of object derived from this class, and sends them the "ButtonPressed" message whenever their particular button is pressed on the GUI. Now consider we have a number of objects which take the "SetBold" method. These object do not inherit this method from a common base. They all simply happen to have the method as part of their interface. Given this, we can write an interesting class template: template class BoldButton : public ButtonClient { public: BoldButton(BoldClient& c) : itsClient(c) {} virtual void ButtonPressed() const {itsClient.SetBold();} private: BoldClient& itsClient; }; Now we can automatically derive a new version of BoldButton for any object that can take the SetBold method: TextString s; BoldButton bb(s); Button b; b.SetClient(bb); // tie the button to the SetBold function of s. ----------------------------------------------- So, templates are more than just mechanisms to make type safe collections. They are a mechanisms for dealing with a completely different sort of polymorphism than normal C++ inheritance.