Template type deduction and specialization
jklp via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed May 20 00:27:52 PDT 2015
On Wednesday, 20 May 2015 at 06:31:13 UTC, Mike Parker wrote:
> I don't understand why this behaves as it does. Given the
> following two templates:
>
> ```
> void printVal(T)(T t) {
> writeln(t);
> }
> void printVal(T : T*)(T* t) {
> writeln(*t);
> }
> ```
>
> I find that I actually have to explicitly instantiate the
> template with a pointer type to get the specialization.
>
> ```
> void main() {
> int x = 100;
> printVal(x);
> int* px = &x;
> printVal(px); // prints the address
> printVal!(int*)(px) // prints 100
> }
> ```
>
> Intuitively, I would expect the specialization to be deduced
> without explicit instantiation. Assuming this isn't a bug (I've
> been unable to turn up anything in Bugzilla), could someone in
> the know explain the rationale behind this?
---
import std.stdio;
void printVal(T)(T t) {
writeln(t);
}
void printVal(T: T)(T* t) {
writeln(*t);
}
void main() {
int x = 100;
printVal(x);
int* px = &x;
printVal(px);
}
---
here it's selected correctly without explicit instantiation. But
honestly i don't know why since the asterisk is removed from the
T it looks quite incorrect.
More information about the Digitalmars-d-learn
mailing list