Ray Gardener said:
Would templates be necessary if C++
had all numeric types inherit from a base "number" class
and had all types inherit from a base "object" class?
(Sorry if this has already been discussed to death)
Ray
That's an interesting question. I will point out that with C++ you actually
could do what you said. The programming community could decide on a set of
wrappers for built in types and a common Object base class for all classes.
Then polymorphism could be used for genericity and templates would be
"unneccesary". Obviously that hasn't happened and if you look at the
standard library I think you will see that templates are used a lot more
than polymorphism. To me that says that in a language where polymorphism and
templates are both readily available programmers will gravitate towards
templates.
There's a lot to like with templates. They are efficient because they avoid
the need for virtual functions. They work for functions as well as classes.
Template containers are non-intrusive and can be used for built in types and
classes both. (Compare to Java containers, which must have one container for
Object types and one for each built in type.)
Note that I did NOT say that polymorphism is "unneccesary" in C++. I
certainly use both. But templates have demonstrated their value on their own
merits and not because of some historical accident in the way C++ was
defined.