Thread overview
Pointer to template types?
Apr 28, 2014
Chris
Apr 28, 2014
bearophile
Apr 28, 2014
Chris
Apr 28, 2014
Chris
Apr 28, 2014
Rene Zwanenburg
Apr 28, 2014
Chris
Apr 28, 2014
bearophile
Apr 28, 2014
Chris
Apr 29, 2014
Jesse Phillips
Apr 29, 2014
Chris
April 28, 2014
I need an array that contains pointers to types created via template. To stick to my usual example:

Person!(string)

How can I make an array with pointers to concrete "instances" of Person!(string)?

Something like this only with pointers, i.e. buf holds pointers to concrete Person!(string)s:

Appender!(Person!(string)[]) buf = appender!(Person!(string)[]);

Thanks in advance.
April 28, 2014
Chris:

> I need an array that contains pointers to types created via template. To stick to my usual example:
>
> Person!(string)
>
> How can I make an array with pointers to concrete "instances" of Person!(string)?

Every template creates a new type, so you can't put them as they are in an array. There are various solutions, none nice. You can try with a wrapper that performs type erasure, or simpler you can make all the same type giving them the string at run-time. Another solution is use OOP, something like (untested and I am not sure):

abstract class APerson {}
class Person(string name) : APerson {}

Bye,
bearophile
April 28, 2014
On Monday, 28 April 2014 at 10:32:18 UTC, bearophile wrote:
> Chris:
>
>> I need an array that contains pointers to types created via template. To stick to my usual example:
>>
>> Person!(string)
>>
>> How can I make an array with pointers to concrete "instances" of Person!(string)?
>
> Every template creates a new type, so you can't put them as they are in an array. There are various solutions, none nice. You can try with a wrapper that performs type erasure, or simpler you can make all the same type giving them the string at run-time. Another solution is use OOP, something like (untested and I am not sure):
>
> abstract class APerson {}
> class Person(string name) : APerson {}
>
> Bye,
> bearophile

So there is no way of filling an array with something like

Person!(string) *pptr;

foreach(person; people) {
  buf ~= &person;
}
April 28, 2014
On Monday, 28 April 2014 at 10:32:18 UTC, bearophile wrote:
> Chris:
>
>> I need an array that contains pointers to types created via template. To stick to my usual example:
>>
>> Person!(string)
>>
>> How can I make an array with pointers to concrete "instances" of Person!(string)?
>
> Every template creates a new type, so you can't put them as they are in an array. There are various solutions, none nice. You can try with a wrapper that performs type erasure, or simpler you can make all the same type giving them the string at run-time. Another solution is use OOP, something like (untested and I am not sure):
>
> abstract class APerson {}
> class Person(string name) : APerson {}
>
> Bye,
> bearophile

So there is no way of filling an array with something like

Person!(string) *pptr;

foreach(person; people) {
   buf ~= &person;
}
April 28, 2014
On Monday, 28 April 2014 at 10:40:49 UTC, Chris wrote:
> So there is no way of filling an array with something like
>
> Person!(string) *pptr;
>
> foreach(person; people) {
>    buf ~= &person;
> }

Person!(string)*[] arr;

Like this?
April 28, 2014
On Monday, 28 April 2014 at 10:44:18 UTC, Rene Zwanenburg wrote:
> On Monday, 28 April 2014 at 10:40:49 UTC, Chris wrote:
>> So there is no way of filling an array with something like
>>
>> Person!(string) *pptr;
>>
>> foreach(person; people) {
>>   buf ~= &person;
>> }
>
> Person!(string)*[] arr;
>
> Like this?

Exactly, just tried it, this works. I tried various types of syntax and just couldn't get it right. Thanks a million.
April 28, 2014
Chris:

> So there is no way of filling an array with something like
>
> Person!(string) *pptr;
>
> foreach(person; people) {
>    buf ~= &person;
> }

So you want an array filled with instances of the same instantiation, sorry, I misunderstood your problem for a more complex one :-)

Bye,
bearophile
April 28, 2014
On Monday, 28 April 2014 at 11:04:17 UTC, bearophile wrote:
> Chris:
>
>> So there is no way of filling an array with something like
>>
>> Person!(string) *pptr;
>>
>> foreach(person; people) {
>>   buf ~= &person;
>> }
>
> So you want an array filled with instances of the same instantiation, sorry, I misunderstood your problem for a more complex one :-)
>
> Bye,
> bearophile

No worries. I didn't make it clear either that it was about syntax.
April 29, 2014
On Monday, 28 April 2014 at 10:40:49 UTC, Chris wrote:
> Person!(string) *pptr;

Just wanted to point out, the above is C style and not recommended.

    Person!(string)* pptr, pptr2, pptr3;

In D the pointer is part of the type not the variable (all three are pointers, unlike C where only the first would be a pointer). By placing the pointer on the variable I do not describe the types correctly.

    Person!(string) *pptr, pptr2, pptr3;

This should help when wanting to use more complex types:

    Person!(string)*[string][][char]* paaaaapp; //:)

used like :(I don't know my precedence):

    *((*paaaaapp)['c'][1]["hello"])
April 29, 2014
On Tuesday, 29 April 2014 at 04:44:39 UTC, Jesse Phillips wrote:
> On Monday, 28 April 2014 at 10:40:49 UTC, Chris wrote:
>> Person!(string) *pptr;
>
> Just wanted to point out, the above is C style and not recommended.
>
>     Person!(string)* pptr, pptr2, pptr3;
>
> In D the pointer is part of the type not the variable (all three are pointers, unlike C where only the first would be a pointer). By placing the pointer on the variable I do not describe the types correctly.
>
>     Person!(string) *pptr, pptr2, pptr3;
>
> This should help when wanting to use more complex types:
>
>     Person!(string)*[string][][char]* paaaaapp; //:)
>
> used like :(I don't know my precedence):
>
>     *((*paaaaapp)['c'][1]["hello"])

Yes, you're right. Thanks for pointing this out. To be sure, I usually use a new line for pointers.