[Issue 8931] New: array/slice assignment causes destruction + postblit instead of opAssign

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 1 10:10:43 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8931

           Summary: array/slice assignment causes destruction + postblit
                    instead of opAssign
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: monarchdodra at gmail.com


--- Comment #0 from monarchdodra at gmail.com 2012-11-01 10:10:38 PDT ---
Basically, given two static arrays of S, or when calling array opSliceAssign,
I'd expect the assignment to trigger element-wise assignment.

However, what happens is that the elements of the original array are destroyed,
and then postblit copies are copied over, element by element:

//----
import std.algorithm;
import std.stdio;

struct S
{
    int i;
    this(this){"post: ".writeln(i);}
    void opAssign(S){"opAssign".writeln();}
    ~this(){"dest: ".writeln(i);}
}

void main()
{
    S[2] a = [S(1), S(2)];
    S[2] b = [S(3), S(4)];
    "begin".writeln();
    a = b;
    "end".writeln();
}
//----
post: 1
post: 2
post: 3
post: 4
begin
post: 3
dest: 1
post: 4
dest: 2
end
dest: 4
dest: 3
dest: 4
dest: 3
//----

opSlice/opSlice assignement will produce the same effect:
//----
void main()
{
    S[] a = [S(1), S(2)];
    S[] b = [S(3), S(4)];
    "begin".writeln();
    a[] = b[];
    "end".writeln();
}
//----

destruction+postblit is not the same as opAssign, so if this is an
"optimization", it is wrong.

-- 
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