Cheaper compile-time tests

bearophile bearophileHUGS at lycos.com
Mon Apr 16 06:15:36 PDT 2012


> Here a new matrixMul is instantiated for each size of the input 
> matrices, but this avoids that:
> ...

So something like this:

import std.stdio;

void foo(T, @generic size_t n, @generic size_t m)(const ref
T[n][m] matrix) {
      foreach (ref row; matrix)
          for (int i = 0; i < n; i++)
              writeln(row[i]);
}

void main() {
      int[2][3] m = [[10, 20],
                     [30, 40],
                     [50, 60]];

      foo(m);
}


means something like this, that is templated only on T:


import std.stdio;

void foo(T)(in size_t n, in size_t m, const T* matrix) {
      for (int j = 0; j < m; j++) {
          auto row = &matrix[j];
          for (int i = 0; i < n; i++)
              writeln(row[i]);
      }
}

void main() {
      int[2][3] m = [[10, 20],
                     [30, 40],
                     [50, 60]];

      foo(m.length, m[0].length, cast(int*)m.ptr);
}


Bye,
bearophile


More information about the Digitalmars-d mailing list