www.digitalmars.com         C & C++   DMDScript  

D - Templates

reply jhenzie mac.com writes:
Can anybody explain the following to me, is it a misprint?

<excerpt>
Multiple instantiations of a TemplateDeclaration with the same 

TemplateParameterList all will refer to the same instantiation. For example:

template TFoo(T) { T f; }
instance TFoo(int) a;
instance TFoo(int) b;
..
a.f = 3;
assert(b.f == 3);	// a and b refer to the same instance of TFoo


This is true even if the TemplateInstances are done in different modules
</excerpt>

Surely the purpose of a template is to create types, not instances.

a and b above, inmo, should be separat instances such that assert(b.f == 3)
fails, unless of course the template is defines template TFoo(T) { static T f; }

Please help
Sep 25 2003
parent jhenzie mac.com writes:
My bad, I suck so bad, these template defs rule.

For anybody else that is having problems with the syntax here is a simple class
template.

template Tmp(T) 
{ 
class Container 
{
T value;
}
}

void  main() 
{
instance Tmp( int ) IntContainer; 

IntContainer.Container a = new IntContainer.Container(); 

a.value = 3;
}


of course you can simplify the syntax further with an alias.

alias IntContainer.Container MyIntContainer;

Justin


In article <bkv80i$19lh$1 digitaldaemon.com>, jhenzie mac.com says...
Can anybody explain the following to me, is it a misprint?

<excerpt>
Multiple instantiations of a TemplateDeclaration with the same 

TemplateParameterList all will refer to the same instantiation. For example:

template TFoo(T) { T f; }
instance TFoo(int) a;
instance TFoo(int) b;
..
a.f = 3;
assert(b.f == 3);	// a and b refer to the same instance of TFoo
This is true even if the TemplateInstances are done in different modules
</excerpt>

Surely the purpose of a template is to create types, not instances.

a and b above, inmo, should be separat instances such that assert(b.f == 3)
fails, unless of course the template is defines template TFoo(T) { static T f; }

Please help
Sep 25 2003