www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template instantiation

reply Trass3r <mrmocool gmx.de> writes:
Got the following code:

public LuaState wrapClass (T) (T instance)
{
	auto ptr = cast (T *) newUserdata ( (T *).sizeof);
	*ptr = instance;

	loadClassMetatable (typeid(T).toString);
	setMetatable (-2);
	return this;
}

Am I right in assuming that a different wrapClass will be created in the 
final executable for each template instantiation (e.g. calls with class 
A,B,C,... instances) and would thus bloat up the executable a bit when 
used with many classes?

Apart from that, couldn't you just use wrapClass (Object instance)? In 
the end each class instance pointer takes up the same amount of memory?!
Mar 20 2009
parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, Mar 20, 2009 at 11:35 AM, Trass3r <mrmocool gmx.de> wrote:
 Got the following code:

 public LuaState wrapClass (T) (T instance)
 {
 =A0 =A0 =A0 =A0auto ptr =3D cast (T *) newUserdata ( (T *).sizeof);
 =A0 =A0 =A0 =A0*ptr =3D instance;

 =A0 =A0 =A0 =A0loadClassMetatable (typeid(T).toString);
 =A0 =A0 =A0 =A0setMetatable (-2);
 =A0 =A0 =A0 =A0return this;
 }

 Am I right in assuming that a different wrapClass will be created in the
 final executable for each template instantiation (e.g. calls with class
 A,B,C,... instances) and would thus bloat up the executable a bit when us=
ed
 with many classes?
Yes.
 Apart from that, couldn't you just use wrapClass (Object instance)? In th=
e
 end each class instance pointer takes up the same amount of memory?!
I'd say yes. It doesn't look like there's any need for a template here. It wouldn't be (Object*).sizeof when allocating the userdata, though; just Object.sizeof.
Mar 20 2009