idiomatic D: what to use instead of pointers in constructing a tree data structure?
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jan 7 15:27:18 PST 2015
On Wednesday, 7 January 2015 at 20:25:10 UTC, Meta wrote:
> struct Tree
> {
> this(string data, Tree[] children...)
> {
> this.data = data;
> this.children = children;
Don't do this without `dup`ing. Quoting the documentation:
> An implementation may construct the object or array instance on
> the stack. Therefore, it is an error to refer to that instance
> after the variadic function has returned:
[...]
> int[] test(int[] a ...)
> {
> return a; // error, array contents invalid after return
> return a[0..1]; // error, array contents invalid after return
> return a.dup; // ok, since copy is made
>}
http://dlang.org/function#variadic
> }
>
> string data;
> Tree[] children;
> }
More information about the Digitalmars-d-learn
mailing list