digitalmars.D.learn - class template as a type
- Karl (13/13) Sep 18 2007 Hi,
- BCS (5/26) Sep 18 2007 void Foo(Test!(somthingToAlias) { }
- Karl (8/40) Sep 18 2007 Thanks for your reply.
- Frits van Bommel (6/22) Sep 19 2007 You could give your template class a non-templated base function, and
- Karl (6/33) Sep 19 2007 Thanks for your reply.
- Jarrett Billingsley (20/23) Sep 19 2007 He meant "non-templated base *class*". So:
- Frits van Bommel (3/12) Sep 19 2007 Yes I did. I'm not quite sure how I managed to typo 'class' to
- Jarrett Billingsley (3/5) Sep 20 2007 It's understandable. The keys are right next to each other.
- Chris Nicholson-Sauls (3/11) Sep 20 2007 You have a [class] key on your keyboard!? ...is that thing available on...
- Karl (2/15) Sep 21 2007 Hehehehe. Guys, back into the subject. The only problem implementing a b...
- Frits van Bommel (3/18) Sep 21 2007 So you make the constructor(s) private and then only classes in the same...
- Karl (3/22) Sep 21 2007 WOW. Great idea, cool man, thanks for everything.
Hi, I've been playing around with templates, i'm a newbie in this matter, i created a class template and i want to use it as a type in functions and create arrays of it. Any help please. Code sample: module hello; template Test(alias T) { class Test { this(){} int x; ... } } void Foo(Test t){} static Test[] myarray;
Sep 18 2007
Reply to Karl,Hi, I've been playing around with templates, i'm a newbie in this matter, i created a class template and i want to use it as a type in functions and create arrays of it. Any help please. Code sample: module hello; template Test(alias T) { class Test { this(){} int x; ... } } void Foo(Test t){} static Test[] myarray;void Foo(Test!(somthingToAlias) { } static Test!(somthingToAlias)[] myarray; look for "Explicit Template Instantiation" in http://www.digitalmars.com/d/template.html
Sep 18 2007
BCS Wrote:Reply to Karl,Thanks for your reply. "somthingToAlias" could be anything right? Lets suppose foo.bar1 or foo.bar2. Then "static Test!(foo.bar1)[] myarray;" would create an array only for elements instantiated from foo.bar1 and i couldn't append an element instantiated from foo.bar2, am i wrong? 1) Then how could i create an array for generic instantiated elements? 2) "void Foo(Test!(somthingToAlias) { }", i think something is missing: - ")". - A variable name, (correct me if wrong): void Foo(Test!(somthingToAlias) sta){ } Thanks in advance.Hi, I've been playing around with templates, i'm a newbie in this matter, i created a class template and i want to use it as a type in functions and create arrays of it. Any help please. Code sample: module hello; template Test(alias T) { class Test { this(){} int x; ... } } void Foo(Test t){} static Test[] myarray;void Foo(Test!(somthingToAlias) { } static Test!(somthingToAlias)[] myarray; look for "Explicit Template Instantiation" in http://www.digitalmars.com/d/template.html
Sep 18 2007
Karl wrote:BCS Wrote:You could give your template class a non-templated base function, and use that as the type of the array elements.void Foo(Test!(somthingToAlias) { } static Test!(somthingToAlias)[] myarray; look for "Explicit Template Instantiation" in http://www.digitalmars.com/d/template.html"somthingToAlias" could be anything right? Lets suppose foo.bar1 or foo.bar2. Then "static Test!(foo.bar1)[] myarray;" would create an array only for elements instantiated from foo.bar1 and i couldn't append an element instantiated from foo.bar2, am i wrong? 1) Then how could i create an array for generic instantiated elements?2) "void Foo(Test!(somthingToAlias) { }", i think something is missing: - ")".Yes, he seems to have made a typo there- A variable name, (correct me if wrong): void Foo(Test!(somthingToAlias) sta){ }Technically the name is optional, but it does come in quite handy when you actually want to *use* the parameter in the function body :).
Sep 19 2007
Frits van Bommel Wrote:Karl wrote:Thanks for your reply. "You could give your template class a non-templated base function, and use that as the type of the array elements." I didn't understood. A small example please. Thanks man.BCS Wrote:You could give your template class a non-templated base function, and use that as the type of the array elements.void Foo(Test!(somthingToAlias) { } static Test!(somthingToAlias)[] myarray; look for "Explicit Template Instantiation" in http://www.digitalmars.com/d/template.html"somthingToAlias" could be anything right? Lets suppose foo.bar1 or foo.bar2. Then "static Test!(foo.bar1)[] myarray;" would create an array only for elements instantiated from foo.bar1 and i couldn't append an element instantiated from foo.bar2, am i wrong? 1) Then how could i create an array for generic instantiated elements?2) "void Foo(Test!(somthingToAlias) { }", i think something is missing: - ")".Yes, he seems to have made a typo there- A variable name, (correct me if wrong): void Foo(Test!(somthingToAlias) sta){ }Technically the name is optional, but it does come in quite handy when you actually want to *use* the parameter in the function body :).
Sep 19 2007
"Karl" <asdf asdf.com> wrote in message news:fcsokc$2uhn$1 digitalmars.com..."You could give your template class a non-templated base function, and use that as the type of the array elements." I didn't understood. A small example please.He meant "non-templated base *class*". So: class TestBase { // put common methods here } class Test(alias T) : TestBase { this() { } int x; ... } void Foo(TestBase t) {} static TestBase[] myArray; compile time. This means that a Test!(int) is just as different from a Test!(float) as an int[] is from a float[].
Sep 19 2007
Jarrett Billingsley wrote:"Karl" <asdf asdf.com> wrote in message news:fcsokc$2uhn$1 digitalmars.com...Yes I did. I'm not quite sure how I managed to typo 'class' to 'function' though..."You could give your template class a non-templated base function, and use that as the type of the array elements." I didn't understood. A small example please.He meant "non-templated base *class*". So:
Sep 19 2007
"Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message news:fct4r0$hl9$1 digitalmars.com...Yes I did. I'm not quite sure how I managed to typo 'class' to 'function' though...It's understandable. The keys are right next to each other.
Sep 20 2007
Jarrett Billingsley wrote:"Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message news:fct4r0$hl9$1 digitalmars.com...You have a [class] key on your keyboard!? ...is that thing available on Amazon? -- Chris Nicholson-SaulsYes I did. I'm not quite sure how I managed to typo 'class' to 'function' though...It's understandable. The keys are right next to each other.
Sep 20 2007
Chris Nicholson-Sauls Wrote:Jarrett Billingsley wrote:Hehehehe. Guys, back into the subject. The only problem implementing a base class is that anyone can create a custom class inherited from the base class, override methods, skip all the base class constructors and then add it to an array or pass it to a function. Then there seems to be no way to check if this custom class implements the class template."Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message news:fct4r0$hl9$1 digitalmars.com...You have a [class] key on your keyboard!? ...is that thing available on Amazon? -- Chris Nicholson-SaulsYes I did. I'm not quite sure how I managed to typo 'class' to 'function' though...It's understandable. The keys are right next to each other.
Sep 21 2007
Karl wrote:Chris Nicholson-Sauls Wrote:So you make the constructor(s) private and then only classes in the same module can inherit from it.Jarrett Billingsley wrote:Hehehehe. Guys, back into the subject. The only problem implementing a base class is that anyone can create a custom class inherited from the base class, override methods, skip all the base class constructors and then add it to an array or pass it to a function. Then there seems to be no way to check if this custom class implements the class template."Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message news:fct4r0$hl9$1 digitalmars.com...You have a [class] key on your keyboard!? ...is that thing available on Amazon? -- Chris Nicholson-SaulsYes I did. I'm not quite sure how I managed to typo 'class' to 'function' though...It's understandable. The keys are right next to each other.
Sep 21 2007
Frits van Bommel Wrote:Karl wrote:WOW. Great idea, cool man, thanks for everything. That surely will work, i don't know why i didn't think about that before...i guess i'm not smart enough.Chris Nicholson-Sauls Wrote:So you make the constructor(s) private and then only classes in the same module can inherit from it.Jarrett Billingsley wrote:Hehehehe. Guys, back into the subject. The only problem implementing a base class is that anyone can create a custom class inherited from the base class, override methods, skip all the base class constructors and then add it to an array or pass it to a function. Then there seems to be no way to check if this custom class implements the class template."Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message news:fct4r0$hl9$1 digitalmars.com...You have a [class] key on your keyboard!? ...is that thing available on Amazon? -- Chris Nicholson-SaulsYes I did. I'm not quite sure how I managed to typo 'class' to 'function' though...It's understandable. The keys are right next to each other.
Sep 21 2007