April 30, 2004
$ dmc vec.d
Assertion failure: 'd' on line 3154 in file 'mtype.c'

when I compile the following program:


struct vec(VALUE, int size){
  enum {nvalues=1}
  VALUE [size]f;
  VALUE* getAt(uint i) {
    return i<size?&f[i]:&f[size-1];
  }
  VALUE opIndex(uint i, VALUE v) {
    return (*getAt(i))=v;
  }
  VALUE opIndex(uint i) {
    return i<size?f[i]:f[size-1];
  }
  template T(zvec) {
    zvec castTo() {
       zvec ret;
       for (int i=max(ret.nvalues-1,size-1);i>=0;i--) {
          ret[i]=this[i];
       }
       return ret;
    }
  }
}
alias vec!(float,1) float1;
alias vec!(float,4) float4;

int main (char[][]args) {
  float1 a;
  float4 b;
  b[0]=1,b[1]=2,b[2]=3,b[3]=4;
  a[1]=3;
  b=a.T(float4).castTo();// <-- taking out this line stops the error

  printf ("%f\n",b[1000]);
  return 0;
}
April 30, 2004
So I tried to rewrite my last code, and I can't get it working for a DIFFERENT reason (though the template way is the right way)

it complains:

"template instance vec!(float,4) vec is not a template declaration"
am I doing something wrong this time? last time I was pretty sure my code was kosher.


struct vec(VALUE, int size){
  enum {nvalues=1}
  VALUE [size]f;
  VALUE* getAt(uint i) {
    return i<size?&f[i]:&f[size-1];
  }
  VALUE opIndex(uint i, VALUE v) {
    return (*getAt(i))=v;
  }
  VALUE opIndex(uint i) {
    return i<size?f[i]:f[size-1];
  }
  vec!(float,4) castFloat4 () {
     vec!(float,4) ret;
     for (int i=max(ret.nvalues-1,size-1);i>=0;i--) {
        ret[i]=this[i];
     }
     return ret;
  }

}
alias vec!(float,1) float1;
alias vec!(float,4) float4;

int main (char[][]args) {
  float1 a;
  float4 b;
  b[0]=1,b[1]=2,b[2]=3,b[3]=4;
  a[1]=3;

  printf ("%f\n",b[1000]);
  return 0;
}