Templates as inheritence omission workaround

R

Ray Gardener

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
 
J

Jeff Schwab

Ray 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?

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

-Jeff
 
R

Ron Natalie

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?

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".
 
C

Cy Edmunds

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.
 
R

Ray Gardener

Would templates be necessary if C++
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
 
J

Jeff Schwab

Ray said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top