Bug 3694 – Template this parameters don't work with operator overloads
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-01-10T15:07:00Z
Last change time
2015-06-09T01:27:02Z
Assigned to
nobody
Creator
tomeksowi
Comments
Comment #0 by tomeksowi — 2010-01-10T15:07:37Z
Testcase:
class A {
int a;
This opAssign(this This)(int a) {
this.a = a;
return this;
}
}
void main() {
A aa = new A;
aa = 4; // oops!
}
Error: template test.A.opAssign(this This) does not match any function template declaration
Error: template test.A.opAssign(this This) cannot deduce template function from argument types !()(int)
-----------------------------------------------
The testcase should compile. Problem is that when operator overloads are used, template this parameters are not inferred. I suspect it's like that for all operator overloads (I tested only for assign and add, though).
When you call the operator functions explicitly, aa.opAssign(4), it compiles.
Comment #1 by gareth.charnock — 2010-03-04T16:23:09Z
I can confirm this bug is still present in DMD 2.040. Test case used:
struct ddouble(int l,int m /*other SI dimensions would go here*/) {
this(double _val) {val=val;}
ddouble!(l+l2,m+m2) opMult(int l2,int m2)(ddouble!(l2,m2) oprand) {
return ddouble!(l+l2,m+m2)(val*oprand.val);
}
ddouble!(l+l2,m+m2) mult(int l2,int m2)(ddouble!(l2,m2) oprand) {
return ddouble!(l+l2,m+m2)(val*oprand.val);
}
/* /,-,+ operators would go here */
double val;
};
void main() {
ddouble!(1,0) length=ddouble!(1,0)(1.0);
ddouble!(0,1) mass =ddouble!(0,1)(1.0);
auto t2=length.mult(mass); //Works
auto t3=length*mass; //Error
}
testcase.d(28): Error: incompatible types for ((length) * (mass)): 'ddouble!(1,0)' and 'ddouble!(0,1)'
Comment #2 by gareth.charnock — 2010-03-08T13:30:49Z
Bug appears to be fixed in DMD 2.041. Both test cases compile fine now.
Comment #3 by tomeksowi — 2010-03-13T03:53:06Z
(In reply to comment #2)
> Bug appears to be fixed in DMD 2.041. Both test cases compile fine now.
Also works here. I'm marking as fixed.