Bug 16332 – std.algorithm.copy uses too restricted array specialization

Status
NEW
Severity
normal
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-07-28T19:17:17Z
Last change time
2024-12-01T16:27:37Z
Assigned to
No Owner
Creator
drug007
Moved to GitHub: phobos#9690 →

Comments

Comment #0 by drug2004 — 2016-07-28T19:17:17Z
In case of array `std.algorithm.copy` uses an array specialization that assumes that that arrays can be bitblt-ed and this assumption is false if array contains elements that can not be bitblt-ed but can be copied by elements. For examples, a structure with indirections with copy ctor or custom `opAssign` can not be bitblt-ed: ``` struct Foo { int[] i; } const(Foo)[] cfoo; Foo[] foo; cfoo.copy(foo); // doesn't compile because // 'copy' uses the array specialization: // foo[] = cfoo[]; ``` but ``` struct Foo { int[] i; ref Foo opAssign(const(Foo) other) { ... } } cfoo.copy(foo); // doesn't compile again because // `copy` again uses the same array specialization // foo[] = cfoo[]; <- this fails again // but should use: // foreach(idx; cfoo.length) // foo[idx] = cfoo[idx]; ``` That is for array `copy` should detect if it can be bitblt-ed or should be copied by element.
Comment #1 by nick — 2023-02-28T18:56:52Z
Complete example: import std.algorithm : copy; struct Foo { int[] i; void opAssign(const(Foo) other) { i = other.i.dup; } } void main() { const(Foo)[] cfoo; foreach (i; 1 .. 4) cfoo ~= Foo([i]); Foo[] foo = new Foo[](3); //cfoo.copy(foo); // NG, should be equivalent to: foreach(idx; 0 .. cfoo.length) foo[idx] = cfoo[idx]; assert(foo == cfoo); }
Comment #2 by robert.schadek — 2024-12-01T16:27:37Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/9690 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB