Thread overview
class template as a type
Sep 19, 2007
Karl
Sep 19, 2007
BCS
Sep 19, 2007
Karl
Sep 19, 2007
Frits van Bommel
Sep 20, 2007
Karl
Sep 20, 2007
Frits van Bommel
September 19, 2007
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;
September 19, 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


September 19, 2007
BCS Wrote:

> 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
> 
> 

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.


September 19, 2007
Karl wrote:
> BCS Wrote:
> 
>> 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?

You could give your template class a non-templated base function, and use that as the type of the array 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 :).
September 20, 2007
Frits van Bommel Wrote:

> Karl wrote:
> > BCS Wrote:
> > 
> >> 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?
> 
> You could give your template class a non-templated base function, and use that as the type of the array 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 :).

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.
September 20, 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;

Remember that unlike Java or C# generics, D's templates are done all at compile time.  This means that a Test!(int) is just as different from a Test!(float) as an int[] is from a float[].


September 20, 2007
Jarrett Billingsley wrote:
> "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:

Yes I did. I'm not quite sure how I managed to typo 'class' to 'function' though...
September 20, 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.


September 20, 2007
Jarrett Billingsley wrote:
> "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. 
> 
> 

You have a [class] key on your keyboard!?  ...is that thing available on Amazon?

-- Chris Nicholson-Sauls