Template type deduction and specialization
Namespace via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed May 20 00:36:21 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?
What about:
----
import std.stdio;
void printVal(T)(T t) {
static if (is(T : U*, U))
printVal(*t);
else
writeln(t);
}
void main() {
int x = 100;
printVal(x);
int* px = &x;
printVal(px);
}
----
More information about the Digitalmars-d-learn
mailing list