insertInPlace differences between compilers

Jesse Phillips via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 12 18:53:05 PST 2014


On Wednesday, 12 November 2014 at 19:25:49 UTC, John McFarlane 
wrote:
>> That makes sense. In the case that `c` is a class, do you 
>> think I'd have any luck if I made it immutable?
> The quick answer is that it doesn't help. DMD still doesn't 
> like me using insertInPlace. This is a little disappointing as 
> immutable is supposed to avoid the need for copying. Then 
> again, I guess that's a compiler - not language - level 
> affordance. Thanks again.

Consider this code:
     void main() {
         import std.range;
         class C { string name; this(string n) { name = n; }}
         struct S { C c; }

         S a = S(new C("a"));
         S b = S(new C("b"));

         auto pC = &b.c;
         b = a;

         assert(pC.name == "a");
         assert(b.c.name == "a");
     }

I have removed the compiler guarantee of not modifying c. What 
you'll notice here is that I've taken a pointer to a memory 
location in your struct after assigning to the struct that memory 
location has been modified to reflect the new class object.

Had the compiler allowed this with the immutable c, by not 
performing a copy, then the assert would have failed which makes 
no sense as you wanted to store 'a' in that memory location.


More information about the Digitalmars-d-learn mailing list