staticMap but with two arguments
    Ali Çehreli 
    acehreli at yahoo.com
       
    Thu Feb  9 19:17:55 UTC 2023
    
    
  
On 2/8/23 12:04, John Chapman wrote:
 > rather than write it manually for each N?
import std.meta : AliasSeq;
template pickArgs(size_t totalElements,
                   size_t argsPerElement,
                   size_t whichElement,
                   args...) {
     alias pickArgs = AliasSeq!();
     static foreach (a; 0 .. argsPerElement) {
         pickArgs = AliasSeq!(pickArgs, args[whichElement + a * 
totalElements]);
     }
}
template staticMapN(size_t N, alias fun, args...)
{
     static assert(N != 0, "N must be non-zero.");
     static assert((args.length % N) == 0,
                   "Mismatched number of arguments");
     enum totalElements = args.length / N;
     alias staticMapN = AliasSeq!();
     static foreach (e; 0 .. totalElements) {
         staticMapN = AliasSeq!(staticMapN,
                                fun!(pickArgs!(totalElements, N, e, args)));
     }
}
// An example struct with some template parameters
struct S(T, size_t length, size_t foo, size_t bar) {
}
// An example template that creates instantiations of the S template
template Instantiate(T, size_t length, size_t foo, size_t bar) {
     alias Instantiate = S!(T, length, foo, bar);
}
// Compile-time argument sets for three instantiations of the S template
alias myTypes = AliasSeq!(int, double, long);
alias mySizes = AliasSeq!(1, 2, 3);
alias myFoos = AliasSeq!(42, 43, 44);
alias myBars = AliasSeq!(100, 200, 300);
// A test with those 4 sets of template arguments
alias result = staticMapN!(4, Instantiate, myTypes, mySizes, myFoos, 
myBars);
pragma(msg, result);
void main() {
}
I could not figure out eliminating the hard-coded 4. Can we introspect 
the parameter list of a template like 'fun' in the example? If we could, 
then we could get 4 that way.
Ali
    
    
More information about the Digitalmars-d-learn
mailing list