called copy constructor in foreach with ref on Range

Rogni maktrefa at gmail.com
Sun Jun 21 19:23:00 UTC 2020


https://pastebin.com/BYcRN8T2

why called copy constructor for ref Range struct type in foreach ?

```
struct MyRange {
     private int current_row = 0;
     private int rows_count = 5;
     @disable this(this); // <===
     bool empty () { return current_row == rows_count; }
     int front() { return current_row*current_row; }
     void popFront() { ++current_row; }
}

struct Container {
     @disable this(this); // <===
     int opApply(int delegate(int) dg) {
         foreach (int row; 0..5) dg(row*row);
         return 1;
     }
}

void foreach_iterable(Type)(ref Type iterable) {
     import std.stdio: writeln;
     foreach (int row_value; iterable) { // test_range.d(20): 
Error: struct test_range.MyRange is not copyable because it is 
annotated with @disable
         row_value.writeln;
     }
}

void call_iterable(Type)() {
     Type iterable;
     foreach_iterable(iterable);
}

void main() {
     call_iterable!Container(); // work
     call_iterable!MyRange(); // does`t work
}
```

ldc2 version: version   1.8.0 (DMD v2.078.3, LLVM 5.0.1)




More information about the Digitalmars-d-learn mailing list