www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23116] New: foreach with opApply infer ref when opApply

https://issues.dlang.org/show_bug.cgi?id=23116

          Issue ID: 23116
           Summary: foreach with opApply infer ref when opApply delegate
                    is templated
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: contact lsferreira.net

For the following code:

```
struct S {
        int opApply(Dg)(scope Dg dg)
        {
                return dg(1);
        }
}

int main()
{
        foreach(int i; S())
        {
                return i;
        }

        return 0;
}
```

The compiler interprets this as:
```
foreach(ref int i; S())
```

Which is wrongly inferred.

--
May 16 2022