How to return a reference to structs?

Andrey Zherikov andrey.zherikov at gmail.com
Thu Nov 4 11:26:30 UTC 2021


I have the following code example:

```d
struct A{}

A[5] a;
ulong[] idx = [1,3,4];

auto get()
{
     return idx.map!(_ => a[_]);
}

foreach(i; 0 .. a.length)
     write(&a[i], " ");
writeln;

foreach(ref b; get())
     write(&b, " ");
writeln;
```

How can I change `get` function so it returns the references to 
the content in `a`?

Is there a good documentation describing how to avoid copying of 
structs in the code?


More information about the Digitalmars-d-learn mailing list