By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,285 Members | 2,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,285 IT Pros & Developers. It's quick & easy.

Templates as inheritence omission workaround

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ray Gardener wrote:
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?


Yes. Those are run-time mechanisms. Templates are a compile-time
mechanism. They serve different purposes.

-Jeff

Jul 22 '05 #2

P: n/a

"Ray Gardener" <ra**@daylongraphics.com> wrote in message news:rX3Hb.841524$9l5.425888@pd7tw2no...
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?


If there was a "super" base class that everything inherited from and
was polymorphic, perhaps not. However, the performance would
be absolutely contrary to the "C++ isn't any slower than it has to be
and certainly not slower than C".

Jul 22 '05 #3

P: n/a
"Ray Gardener" <ra**@daylongraphics.com> wrote in message
news:rX3Hb.841524$9l5.425888@pd7tw2no...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4

P: n/a
> > 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?
Yes. Those are run-time mechanisms. Templates are a

compile-time mechanism. They serve different purposes.


Interesting... so would it also be possible to make
a C++ compiler that effected each class with its own
standalone type, implementing all polymorphism with
templates in the emitted object code. The size would
be huge, but all the virtual function overhead
would disappear.

Ray
Jul 22 '05 #5

P: n/a
Ray Gardener wrote:
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?


Yes. Those are run-time mechanisms. Templates are a


compile-time
mechanism. They serve different purposes.

Interesting... so would it also be possible to make
a C++ compiler that effected each class with its own
standalone type, implementing all polymorphism with
templates in the emitted object code. The size would
be huge, but all the virtual function overhead
would disappear.


Yes, it's entirely possible. See the Josuttis book on templates.
However, such programs require a lot of information to be available at
compilation. Run-time polymorphism is still useful in many cases.

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.