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

ryuukk_ ryuukk.dev at gmail.com
Tue Oct 3 15:31:19 UTC 2023


On Tuesday, 3 October 2023 at 15:12:34 UTC, Joel wrote:
> 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);
> }
> ```

Remove:
It didn't segfault for me, but it still didn't print what's in 
the ``ints``, but if you remove the following code:



```
    T[] opIndex() {
         return this.array;
     }
     auto opDollar() {
         return length;
     }
```

..then it'll work

Sounds like a bug.. i don't use these D features, maybe someone 
else have more insights?

Either way, what a terrible experience



More information about the Digitalmars-d-learn mailing list