Pointer to template types?

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 28 03:32:17 PDT 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


More information about the Digitalmars-d-learn mailing list