Bug 5958 – const/immutable ignored passing to opAssign()

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2011-05-08T11:23:00Z
Last change time
2011-05-10T16:55:57Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
marcianx

Comments

Comment #0 by marcianx — 2011-05-08T11:23:17Z
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 }
Comment #1 by smjg — 2011-05-10T16:55:57Z
*** This issue has been marked as a duplicate of issue 5493 ***