On Wednesday, 20 May 2015 at 09:24:28 UTC, Daniel Kozák wrote:

On Wed, 20 May 2015 06:31:11 +0000
Mike Parker via Digitalmars-d-learn <[email protected]>
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?

Because it cannot deduce type T:

try this:

void printVal(T : T*)(T* t) {
    writeln(*t);
}

void main() {
        int x = 100;
        int* px = &x;
        printVal(px);
}

It will print error.

My advise is not to use T:T* or T:T[] it works only when explicitly
instantiate. Is better use T:M*,M or T:M[], M because it works
automaticly and you have both types available.

import std.stdio;

void printVal(T)(T t) {
    writeln(t);
}

void printVal(T:M*,M)(T t) {
    writeln(*t);
}

void main() {
    int x = 100;
    printVal(x);
    int* px = &x;
    printVal(px);        // prints the 100
}

DOCS: http://dlang.org/template.html#function-templates
says: Function template type parameters that are to be implicitly deduced may not have specializations:

Reply via email to