void main() {
int[2] foo;
foo = foo;
}
object.Exception: overlapping array copy
This is a ridiculous limitation and is bugs waiting to happen. (For example, Bug 4789.) Even if this requires a simple runtime check before calling memcpy() or whatever, IMHO it's worth it because the cost of a single pointer comparison is negligible in almost all cases and for tiny static arrays (where it might not be negligible) the compiler could just generate regular assignment instructions that are safe for the overlapping/identical case instead of using something like memcpy().
Comment #1 by bearophile_hugs — 2010-09-03T04:39:48Z
The only correct syntax to write that code is this, see bug 3971
void main() {
int[2] foo;
foo[] = foo[];
}
Comment #5 by bearophile_hugs — 2014-06-06T07:40:34Z
(In reply to David Simcha from comment #2)
> Yes, and this is a nightmare for generic code.
A recent bug in my code (I have used a loop to avoid the overhead of calling the runtime function):
void main() {
int[3] a, b, c;
foreach (immutable i; 0 .. a.length)
c = a[i] + b[i];
}
The correct foreach body is instead:
c[i] = a[i] + b[i];
So I think there are disadvantages at not requiring a [] syntax for all array-wide operations.