digitalmars.D.learn - Pointer to template types?
- Chris (9/9) Apr 28 2014 I need an array that contains pointers to types created via
- bearophile (11/16) Apr 28 2014 Every template creates a new type, so you can't put them as they
- Chris (6/24) Apr 28 2014 So there is no way of filling an array with something like
- Chris (6/24) Apr 28 2014 So there is no way of filling an array with something like
- Rene Zwanenburg (3/8) Apr 28 2014 Person!(string)*[] arr;
- Chris (3/13) Apr 28 2014 Exactly, just tried it, this works. I tried various types of
- bearophile (6/11) Apr 28 2014 So you want an array filled with instances of the same
- Chris (3/16) Apr 28 2014 No worries. I didn't make it clear either that it was about
- Jesse Phillips (13/14) Apr 28 2014 Just wanted to point out, the above is C style and not
- Chris (3/17) Apr 29 2014 Yes, you're right. Thanks for pointing this out. To be sure, I
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
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
On Monday, 28 April 2014 at 10:32:18 UTC, bearophile wrote:Chris:So there is no way of filling an array with something like Person!(string) *pptr; foreach(person; people) { buf ~= &person; }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
On Monday, 28 April 2014 at 10:32:18 UTC, bearophile wrote:Chris:So there is no way of filling an array with something like Person!(string) *pptr; foreach(person; people) { buf ~= &person; }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
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
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:Exactly, just tried it, this works. I tried various types of syntax and just couldn't get it right. Thanks a million.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
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
On Monday, 28 April 2014 at 11:04:17 UTC, bearophile wrote:Chris:No worries. I didn't make it clear either that it was about syntax.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
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
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:Yes, you're right. Thanks for pointing this out. To be sure, I usually use a new line for pointers.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 29 2014