D - Templates
- jhenzie mac.com (16/16) Sep 25 2003 Can anybody explain the following to me, is it a misprint?
- jhenzie mac.com (20/36) Sep 25 2003 My bad, I suck so bad, these template defs rule.
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
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 TFooThis 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