473,804 Members | 3,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates as inheritence omission workaround

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
5 1422
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

"Ray Gardener" <ra**@daylongra phics.com> wrote in message news:rX3Hb.8415 24$9l5.425888@p d7tw2no...
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
"Ray Gardener" <ra**@daylongra phics.com> wrote in message
news:rX3Hb.8415 24$9l5.425888@p d7tw2no...
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
"unneccesar y". 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 "unneccesar y" 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
> > 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2168
by: Mark | last post by:
Hopefully somebody can help me with this very frustrating problem. In Dreamweaver I have a template page and a page created from this template called Home.aspx. In Visual Studio I have a class inherited from System.Web.UI.Page which represents the template (including all components and associated events defined in the template). Then I have a codebehind file for Home.aspx that defines a class (Home) that includes controls specific to...
5
2436
by: jimmy | last post by:
I am trying to simulate typedef template similar to the suggestion of Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm However when implementing typedef templates according to his suggestion, I run into type inference problems when passing typedef templates as arguments to template functions. It seems the compiler (gcc 4.0) cannot resolve the typedef template with an instantiation of that typedef template. Below is...
4
1957
by: KK | last post by:
Hello all, I have class 'atr' which is based on templates. My idea was to initialize it two scenarios 1. std::string case 2. other data types I have defined the Init for the above different cases accordingly (code shown below). However, I get compiler error. Please help me resolve it. Thanks.
4
2482
by: Sacha | last post by:
I'm aware, that up to date, "typedef templates" are not defined within the C++ standard. The seemingly common workaround is this: template <class T> struct MyTypeDef { /* ultimately I need something like this: MyOtherClass<T> Type; but let's keep it simple for the moment */
3
2139
by: mario.rossi | last post by:
Hello! I would like to write a pointer to type template that behaves exactly and completely like normal pointers, which I will use as starting point for other kinds of pointers that hold extra data (for example, one of my aims is to have 64bit pointers on a 32bit machine, and ignore the highmost 32bits of the address, while still keeping it in structures and function parameters, so I can start to migrate my 32bit OS to 64bit, although I...
23
2612
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
3
2058
by: tschwartz | last post by:
I'm trying to write a stylesheet which removes nodes which are empty as a result of other template processing. For example, given the following xml, I'd like to: - remove all "b" elements - remove all "a" elements which, as a result of "b" element removal, now have no children so, starting with:
4
1332
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with templates. I've read up some FAQs, but still cannot seem to find the source of my problem! I have three files: Bezier.h, Bezier.cpp and main.cpp. Here are stripped down versions: --- Bezier.h---
4
3353
by: Mukesh_Singh_Nick | last post by:
Does PHP support multiple inheritence? If not, how would one workaround it when one needs a class to inherit from multiple classes?
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.