How to return a reference to structs?

Ali Çehreli acehreli at yahoo.com
Fri Nov 5 19:49:50 UTC 2021


On 11/5/21 11:44 AM, kdevel wrote:
 > On Thursday, 4 November 2021 at 11:26:30 UTC, Andrey Zherikov wrote:
 >> I have the following code example:
 >
 > [...]
 >
 >> A[5] a;
 >> ulong[] idx = [1,3,4];
 >
 > [...]
 >
 >>     return idx.map!(_ => a[_]);
 >
 > How can one make the type of idx's elements portable (compile with -m32
 > to see what the problem is)?

Indexes are typed as size_t, which is ulong for 64-bit and uint for 
32-bit. If idx elements were indeed indexes, it should have been typed 
as size_t:

import std.algorithm;
import std.stdio;

struct A{}

A[5] a;
size_t[] idx = [1,3,4];  // <-- Here

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

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

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

Ali



More information about the Digitalmars-d-learn mailing list