[Issue 6652] New: foreach parameter with number range is always ref
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Sep 12 10:19:34 PDT 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6652
Summary: foreach parameter with number range is always ref
Product: D
Version: D2
Platform: Other
OS/Version: FreeBSD
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: dawg at dawgfoto.de
--- Comment #0 from dawg at dawgfoto.de 2011-09-12 10:19:15 PDT ---
void main() {
size_t cnt;
foreach(ulong n; 0 .. 10)
{
++n;
++cnt;
}
assert(cnt == 10);
cnt = 0;
foreach(ref ulong n; 0 .. 10)
{
++n;
++cnt;
}
assert(cnt == 5);
}
---
As this is rewritten in terms of a for loop all writes to n will
alter the loop.
A hidden copy of n is needed for non-ref parameters to match the range
foreach semantic.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list