www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template instance does not match template declaration

reply Marc <mailnawman gmail.com> writes:
Hello,
I'm trying to declare a templated member function that takes a 
value of size_t N. A simple example to reproduce what Im trying 
to do is the following:

```d
import std.stdio;
void getCol(N: size_t)() {
    return N;
}

void main() {
    // Call
    writeln(getCol!0());
}
```
I get the following error: `Error: template instance `getCol!0` 
does not match template declaration `getCol(N : ulong)()`
    writeln(getCol!0());
            ^
Error dmd failed with exit code 1.`

Can anyone please help me getting it to work?
Aug 15
next sibling parent reply 0xEAB <desisma heidel.beer> writes:
On Friday, 15 August 2025 at 12:02:19 UTC, Marc wrote:
 Can anyone please help me getting it to work?
It seems like you’ve accidentally used the wrong syntax for the template parameter. To make the parameter a value parameter of type `size_t`, write: ``` size_t getCol(size_t N)() { return N; } ``` Also, your return type was off. Can’t return data from a `void` function.
Aug 15
next sibling parent 0xEAB <desisma heidel.beer> writes:
On Friday, 15 August 2025 at 12:18:30 UTC, 0xEAB wrote:
 It seems like you’ve accidentally used the wrong syntax for the 
 template parameter.
With the syntax from your code snippet you’ve put a constraint on the types that can be substituted for `N` when instantiating the template. ```d N getCol(N : size_t)(N n) { return n; } void main() { writeln(getCol(0)); // works writeln(getCol!uint(0)); // works writeln(getCol(0f)); // fails writeln(getCol!float(0)); // fails } ``` This kind of constraint is commonly used with class-based polymorphism where it allows you to restrict the type to a certain class including subclasses. ```d class Foo {} class Bar {} class Sub : Foo {} auto doSth(T : Foo)(T value) { return value; } void main() { doSth(new Foo()); doSth(new Sub()); doSth(new Bar()); // fails } ```
Aug 15
prev sibling parent Marc <mailnawman gmail.com> writes:
On Friday, 15 August 2025 at 12:18:30 UTC, 0xEAB wrote:
 On Friday, 15 August 2025 at 12:02:19 UTC, Marc wrote:
 Can anyone please help me getting it to work?
It seems like you’ve accidentally used the wrong syntax for the template parameter. To make the parameter a value parameter of type `size_t`, write: ``` size_t getCol(size_t N)() { return N; } ``` Also, your return type was off. Can’t return data from a `void` function.
Thanks. It work now.
Aug 15
prev sibling parent reply Sergey <kornburn yandex.ru> writes:
On Friday, 15 August 2025 at 12:02:19 UTC, Marc wrote:
 Hello,
 I'm trying to declare a templated member function that takes a 
 value of size_t N. A simple example to reproduce what Im trying 
 to do is the following:

 ```d
 import std.stdio;
 void getCol(N: size_t)() {
    return N;
 }

 void main() {
    // Call
    writeln(getCol!0());
 }
 ```
 I get the following error: `Error: template instance `getCol!0` 
 does not match template declaration `getCol(N : ulong)()`
    writeln(getCol!0());
            ^
 Error dmd failed with exit code 1.`

 Can anyone please help me getting it to work?
D doesn't use ":" for types ```d import std; size_t getCol(size_t N)() { return N; } void main() { // Call writeln(getCol!0()); } ```
Aug 15
parent Marc <mailnawman gmail.com> writes:
On Friday, 15 August 2025 at 12:20:16 UTC, Sergey wrote:
 On Friday, 15 August 2025 at 12:02:19 UTC, Marc wrote:
 Hello,
 I'm trying to declare a templated member function that takes a 
 value of size_t N. A simple example to reproduce what Im 
 trying to do is the following:

 ```d
 import std.stdio;
 void getCol(N: size_t)() {
    return N;
 }

 void main() {
    // Call
    writeln(getCol!0());
 }
 ```
 I get the following error: `Error: template instance 
 `getCol!0` does not match template declaration `getCol(N : 
 ulong)()`
    writeln(getCol!0());
            ^
 Error dmd failed with exit code 1.`

 Can anyone please help me getting it to work?
D doesn't use ":" for types ```d import std; size_t getCol(size_t N)() { return N; } void main() { // Call writeln(getCol!0()); } ```
Thanks for your reply. It solved my problem.
Aug 15