T[] opIndex() Error: .. signal 11

Joel joelcnz at gmail.com
Tue Oct 3 15:12:34 UTC 2023


The following program crashes, but doesn’t if I change (see 
title) T[] to auto. The program doesn’t even use that 
method/function. What’s the story?

```d
// Adding program - literal functions

import std;

struct List(T) {
     class Node {
         T data;
         Node next;
         this(T data) {
             this.data=data;
         }
     }
     string title;
     size_t length;
     Node head;
     this(string title, T[] data...) {
         this.title=title;
         length=data.length;
         if (length) {
             head=new Node(data[0]);
             auto cur=head;
             data[1..$].each!((d) {
                 cur.next=new Node(d);
                 cur=cur.next;
             });
         }
     }
     bool empty() { return head is null; }
     auto ref front() { return head.data; }
     void popFront() { head=head.next; }
     T[] opIndex() {
         return this.array;
     }
     auto opDollar() {
         return length;
     }
}

void main(string[] args) {
     args.popFront;
     List!int ints;
     if (args.length) {
         ints=List!int(args[0], args[1..$].to!(int[]));
     } else{
         ints=List!int("Car, and Date numbers", 1979,9,3,4,5);
     }
     stdout.write(ints.title, ": ");
     ints.each!((i, d) {
         stdout.write(d, i+1<ints.length ? "+" : "=");
     });
     writeln(ints.sum);
}
```


More information about the Digitalmars-d-learn mailing list