I don't quite get foreach ref
Steven Schveighoffer
schveiguy at yahoo.com
Mon Aug 9 05:10:12 PDT 2010
On Sun, 08 Aug 2010 06:01:20 -0400, Tomek Sowiński <just at ask.me> wrote:
> 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.
Historically, when using opApply to implement foreach, opApply's delegate
must take all ref arguments. This is a source of pain because if you
don't want to ref your data, you had to make temporary copies.
I don't know why ranges have propagated this silliness.
You should file a bug. Also vote for mine:
http://d.puremagic.com/issues/show_bug.cgi?id=2443
-Steve
More information about the Digitalmars-d
mailing list