Bug 7513 – [TDPL] opAssign examples don't work as described

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-15T17:34:00Z
Last change time
2012-12-02T10:59:44Z
Assigned to
nobody
Creator
hoganmeier

Comments

Comment #0 by hoganmeier — 2012-02-15T17:34:20Z
import std.algorithm; struct Widget { private int[] array; this(uint length) { array = new int[length]; } this (this) { array = array.dup; } ref Widget opAssign(ref Widget rhs) { array = rhs.array.dup; return this; } ref Widget opAssign(Widget rhs) { swap(array, rhs.array); return this; } } void main() { Widget w; w = Widget(50); // line 30 } Two issues here: 1. Comment out the 2nd opAssign and the compiler should give an error, but it doesn't. (TDPL: // Cannot bind an rvalue of type Widget to ref Widget!) 2. TDPL's solution is 2 opAssigns, but this results in: $ dmd -release -O -run test2.d test2.d(30): Error: overloads ref Widget(ref Widget rhs) and ref Widget(Widget rhs) both match argument list for opAssign test2.d(30): Error: function test2.Widget.opAssign called with argument types: ((Widget)) matches both: test2.Widget.opAssign(ref Widget rhs) and: test2.Widget.opAssign(Widget rhs)
Comment #1 by andrej.mitrovich — 2012-12-02T10:59:44Z
The issue is with struct literals being lvalues. *** This issue has been marked as a duplicate of issue 9069 ***