Bug 5886 – Template this parameter cannot be made implicit, when other parameters are explicitly given
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-04-25T14:10:00Z
Last change time
2013-04-22T23:55:35Z
Keywords
patch, rejects-valid
Assigned to
nobody
Creator
kennytm
Comments
Comment #0 by kennytm — 2011-04-25T14:10:27Z
Test case:
--------------------------------------
struct K {
void get1(this T)() const {
pragma(msg, T);
}
void get2(int N=4, this T)() const {
pragma(msg, N, " ; ", T);
}
}
void main() {
K km;
const(K) kc;
immutable(K) ki;
km.get1; // OK
kc.get1; // OK
ki.get1; // OK
km.get2; // OK
kc.get2; // OK
ki.get2; // OK
km.get2!(1, K); // Ugly
kc.get2!(2, const(K)); // Ugly
ki.get2!(3, immutable(K)); // Ugly
km.get2!8; // Error
kc.get2!9; // Error
ki.get2!10; // Error
}
--------------------------------------
K
const(K)
immutable(K)
4 ; K
4 ; const(K)
4 ; immutable(K)
1 ; K
2 ; const(K)
3 ; immutable(K)
x.d(23): Error: template instance get2!(8) does not match template declaration get2(int N = 4,this T)
--------------------------------------
The template this parameter is supposed to pick up the type of this at the instantiation site, and should be implicitly defined. But when other template parameters are present, and they are not implicitly determined, the compiler will somehow ignore the fact that T is a TemplateThisParameter, and requires user to fill it out manually.
This also affects operator overloading, e.g.
--------------------------------------
import std.stdio;
struct K {
int opUnary(string op:"-", this T)() const {
return 0;
}
}
void main() {
K km;
auto a = -km; // Error
}
--------------------------------------
x.d(11): Error: template instance opUnary!("-") does not match template declaration opUnary(string op : "-",this T)
--------------------------------------
I think issue 5393 is a special case of this too.