Template to create a type and instantiate it

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 4 23:44:29 PST 2016


On 05/02/16 8:41 PM, cy wrote:
> I'm guessing I have to use a "mixin" mixin for this, but... there's no
> way to do something like this is there?
>
> template TFoo(T) {
>
> struct T {
>    int a;
>    int b;
> }
>
> T obj;
> }
>
> TFoo!Derp;
> Derp bar;
>
> Neither templates, nor mixin templates seem capable of this. Easy enough
> to use mixin, with tokenized string literal format, I think. I just
> hesitate to make opaque string evaluation a thing if some more
> meaningful method exists.

That code is completely wrong anyway.
But you could do:

alias Derp = TFoo;
Derp obj;

struct TFoo {
	int a, b;
}

Of course you can make TFoo a template so that alias could initiate it 
with your arguments.


More information about the Digitalmars-d-learn mailing list