Cannot interpret struct at compile time

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat May 7 16:39:55 PDT 2011


One simplistic solution is to use alias this to simulate the same type:

struct Foo
{
    int x, y;
}

string structClone(T)()
{
    return "struct " ~ T.stringof ~ "_ { "
        ~ T.stringof ~ " _inner;
        alias _inner this;
        this(T...)(T t) { _inner = typeof(_inner)(t);  } };";
}

void main()
{
    mixin(structClone!Foo);
    Foo_ foo = Foo_(1, 2);

    assert(foo.x == 1);
    assert(foo.y == 2);
}

Field initializations and ctors will work thanks to the templated ctor
that just forwards to the _inner struct.


More information about the Digitalmars-d-learn mailing list