🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #17

Templates & Down-Casting

#17
From: Mike Tiller
Date: Fri, 30 Oct 1992 15:44
48 lines
1291 bytes
I realize the g++ implementation of templates is incomplete, but
I have a question about the template mechanism in general.

Lets say you have classes like:

class	Base
{
...
};

class	Sub : public Base
{
...
};

template<class T>
class Array
{
...
};

And the following code:

void Sort(Array<Base *>& ba)
{
...
}

main()
{
	Array<Base *>	ba;
	Array<Sub *>	sa;

	Sort(ba);
	Sort(sa); // This line gives you an error
}

My question is, is there some clever design trick to circumvent
this restriction?  Does anyone know what the "standard" has to
say about this?  I looked in the ARM, didn't seem to say much on
this subject.  I could see why this would cause problems if
the sizeof(Base *) != sizeof(Sub *).  Is there a reason for
restrictions like this?  (I am certainly not an expert on such
things)

Thanks

Michael Tiller
Technical Details
Message-ID: <BwxyDE.FJE@news.cso.uiuc.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!ux1.cso.uiuc.edu!news.cso.uiuc.edu!quattro!tiller

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.help
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!ux1.cso.uiuc.edu!news.cso.uiuc.edu!quattro!tiller
From: tiller@solace.me.uiuc.edu (Mike Tiller)
Subject: Templates & Down-Casting
Message-ID: <BwxyDE.FJE@news.cso.uiuc.edu>
Sender: tiller@quattro (Mike Tiller)
Organization: University of Illinois at Urbana
Date: Fri, 30 Oct 1992 15:44:01 GMT
Keywords: templates
Lines: 52