how to do this meta-programming? print the address of random element's address of a variable length of arrays?

Paul Backus snarwin at gmail.com
Sat Sep 12 14:31:59 UTC 2020


On Saturday, 12 September 2020 at 03:19:23 UTC, mw wrote:
> I.e. I want to learn the generic meta-programming way to 
> assemble such parameter list (&(x[i], &(y[j])) at compile time, 
> it is possible?

It's possible if you use a helper function. Here's how:

import std.meta: allSatisfy;
import std.traits: isArray;

void printRandomElemAddr(Arrays...)(Arrays arrays)
     if (allSatisfy!(isArray, Arrays))
{
     auto randomElemAddr(size_t i)()
         if (i < arrays.length)
     {
         import std.random: uniform;

         return &arrays[i][uniform(0, $)];
     }

     import std.stdio: writeln;
     import std.meta: staticMap, aliasSeqOf;
     import std.range: iota;

     writeln(staticMap!(randomElemAddr, 
aliasSeqOf!(iota(arrays.length))));
}

void main()
{
     int[] a = [1];
     int[] b = [2, 3];
     double[] c = [4, 5, 6];

     printRandomElemAddr(a);
     printRandomElemAddr(a, b);
     printRandomElemAddr(a, b, c);
}


More information about the Digitalmars-d-learn mailing list