Arrays and struct assignment, pt. 2

David Nadlinger via Digitalmars-d digitalmars-d at puremagic.com
Sat Aug 1 18:50:48 PDT 2015


Here is another example where neither opAssign nor the dtor are 
called for an assignment (unless a postblit is added too):

---
uint dtorCount;

struct S {
   uint x;
   void opAssign(const ref S rhs) { assert(false, "Not called"); }
   ~this() { ++dtorCount; }
}

void main() {
   S[] a;
   a.length = 1;
   a[0].x = 42; // Some random non-init value

   a[] = S.init;

   assert(a[0].x == 0); // As expected, the value has been reset
   assert(dtorCount == 0); // Passes?!?
}
---


Again, am I missing something obvious here? I can't quite believe 
that struct lifetime would have been quite as broken for so long.

  – David


More information about the Digitalmars-d mailing list