Bug 4103 – opAssign signature rules not enforced on templated opAssign
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-04-19T00:59:00Z
Last change time
2015-02-09T14:35:20Z
Keywords
accepts-invalid, spec
Assigned to
nobody
Creator
bugzilla
Comments
Comment #0 by bugzilla — 2010-04-19T00:59:09Z
The "Operator Overloading" page of the spec says that:
"...the following parameter signatures for opAssign
are not allowed:
...
opAssign(T)
...
where T is the same type as the aggregate type A..."
However, the following compiles:
import std.stdio;
struct S
{
S opAssign(T)(T t)
{
writeln(T.stringof);
return this;
}
}
void main()
{
S a, b;
a = b;
}
When run, it prints 'S'.
Comment #1 by bugzilla — 2010-04-19T01:01:07Z
I am using DMD 2.043.
Comment #2 by k.hara.pg — 2015-02-09T14:35:20Z
D2 spec is updated for struct.
http://dlang.org/operatoroverloading#assignment
> For struct types, operator overloading for the identity assignment is allowed.
...
> However for class types, identity assignment is not allowed.
And for class types, identity opAssign definition is properly checked, even if it's template.
class C
{
C opAssign(T)(T t) // line 3
{
writeln(T.stringof);
return this;
}
}
void main() {}
Output:
test.d(3): Error: class test.C identity assignment operator overload is illegal