[Issue 11934] New: Allow `ref` in `foreach` over range iff `front` returns by `ref`

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 15 23:45:20 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=11934

           Summary: Allow `ref` in `foreach` over range iff `front`
                    returns by `ref`
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: verylonglogin.reg at gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg at gmail.com> 2014-01-16 11:45:17 MSK ---
Test code:
---
struct S1
{
    @property bool empty();
    @property int front();
    void popFront();
}

struct S2
{
    @property bool empty();
    @property ref int front();
    void popFront();
}

void main()
{
    static assert( __traits(compiles, { foreach(    n; S1()) { } }));
    static assert(!__traits(compiles, { foreach(ref n; S1()) { } })); // fails
    static assert( __traits(compiles, { foreach(ref n; S2()) { } }));
}
---

When `front` doesn't return by `ref` it means returned value is a temporary and
its modification is nonsense. In such case `ref` in `foreach` should be
rejected.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list