I don't quite get foreach ref

Tomek Sowiński just at ask.me
Sun Aug 8 03:01:20 PDT 2010


struct DumbRange {
    int front() { return 5; }             // rvalue
    void popFront() { --counter; }
    bool empty() { return counter > 0; }
    uint counter = 10;
}

void clear(ref int a) { a = 0; }

void main() {
    auto r = DumbRange();

    // Doesn't compile (and good): r.front() is not an lvalue
    clear(r.front);

    // Confusingly has no effect. Yet, compiles -- should it?
    foreach(ref a; r) a = 0;
}

I don't know what to make of it, the D page says nothing on foreach ref on rvalues.


Tomek


More information about the Digitalmars-d mailing list