www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointer to template types?

reply "Chris" <wendlec tcd.ie> writes:
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.
Apr 28 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Apr 28 2014
next sibling parent "Chris" <wendlec tcd.ie> writes:
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; }
Apr 28 2014
prev sibling parent reply "Chris" <wendlec tcd.ie> writes:
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; }
Apr 28 2014
next sibling parent reply "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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?
Apr 28 2014
parent "Chris" <wendlec tcd.ie> writes:
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.
Apr 28 2014
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Apr 28 2014
parent "Chris" <wendlec tcd.ie> writes:
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.
Apr 28 2014
prev sibling parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
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"])
Apr 28 2014
parent "Chris" <wendlec tcd.ie> writes:
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.
Apr 29 2014