Bug 2444 – structs should allow calling the normal blitting function when overriding opAssign
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2008-11-06T09:55:00Z
Last change time
2016-07-05T12:36:19Z
Assigned to
nobody
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2008-11-06T09:55:38Z
In D1, if I overload opAssign in a struct, I cannot create an overload that takes the struct as an argument. This is marked as illegal.
However, in a recent version of D2 (not sure which one), this became legal. However it has a bad side effect. If I want to overload opAssign for an argument other than the struct, for example an int, then it masks the overload that just does the blitting. I have to re-implement the blitting version.
I understand that an author may want to control opAssign to the point of disallowing simple blitting copy. But if desired, it is a PITA to do the blitting manually. It would be nice to be able to alias the original function, or flag the struct somehow so that the compiler will use the blitting function when assigning one struct to another.
Comment #1 by andrej.mitrovich — 2012-12-01T16:21:12Z
I think you can implement this now as:
struct Foo
{
void opAssign(const Foo rhs)
{
this.tupleof = rhs.tupleof;
}
}
Maybe it should be added to the docs.
Comment #2 by mathias.lang — 2016-07-03T22:44:51Z
So, IIUC, your test case would be:
```
struct Foo
{
int a;
~this () {}
void opAssign (int i) { }
}
void main ()
{
Foo f;
f = 42;
assert(f.a == 0);
f = Foo(45);
assert(f.a == 45);
}
```
This currently compiles & works as expected. Nowadays the compiler always generate the required `opAssign` (which is the identity opAssign). Closing as 'worksforme'.
Comment #3 by schveiguy — 2016-07-05T12:36:19Z
Seems fine with me. I tested all the way back to 2.040, and it was working there as well.