Standard struct constructors for the heap?

Andrej Mitrovic andrej.mitrovich at gmail.com
Wed May 16 17:44:49 PDT 2012


On 5/17/12, bearophile <bearophileHUGS at lycos.com> wrote:
> snip

Mixin workaround:

import std.conv;

@property string makeCtors(T)()
{
    T t;
    string res;
    foreach (i; 0 .. typeof(t.tupleof).length)
    {
        res ~= "this(typeof(this.tupleof[0.."
                ~ to!string(i+1)
                ~ "]) tup) { this.tupleof[0.."
                ~ to!string(i+1) ~ "] = tup; }\n";
    }

    return res;
}

struct Node
{
    mixin(makeCtors!Node);
    int data;
    Node* next;
}

void main() {
    Node* n1 = new Node(10); // OK
    Node* n2 = new Node(10, null); // OK
}


More information about the Digitalmars-d mailing list