[Issue 5958] New: const/immutable ignored passing to opAssign()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun May 8 11:27:14 PDT 2011


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

           Summary: const/immutable ignored passing to opAssign()
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: marcianx at gmail.com


--- Comment #0 from marcianx at gmail.com 2011-05-08 11:23:17 PDT ---
I saw examples in the D Programming Language book (Page 256) where opAssign()
took a ref argument instead of a const ref. Just to test, I made example below
which incorrectly compiles and runs with both gdc and dmd for D2. The example
also works with immutable replaced by const.

import std.stdio;

struct Foo
{
   auto ref opAssign(ref Foo s)
   {
       s.x = 4; // modifying mutable object !!!
       x = s.x;
       return this;
   }

   private int x = 0;
}

void main(string[] args)
{
   immutable(Foo) foo = Foo();
   auto foo2 = Foo();
   writefln("(immutable!) foo.x = %s", foo.x); // prints 0
   foo2 = foo; // allowed immutable RHS ???
   writefln("(immutable?) foo.x = %s", foo.x); // prints 4
}

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