Thread overview
Template instance does not match template declaration
5 days ago
Marc
5 days ago
0xEAB
5 days ago
0xEAB
5 days ago
Marc
5 days ago
Sergey
5 days ago
Marc
5 days ago

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:

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!0does not match template declarationgetCol(N : ulong)() writeln(getCol!0()); ^ Error dmd failed with exit code 1.

Can anyone please help me getting it to work?

5 days ago

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.

5 days ago

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:

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!0does not match template declarationgetCol(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

import std;

size_t getCol(size_t N)() {
   return N;
}

void main() {
   // Call
   writeln(getCol!0());
}
5 days ago

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.

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.

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
}
5 days ago

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.

5 days ago

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:

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!0does not match template declarationgetCol(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

import std;

size_t getCol(size_t N)() {
   return N;
}

void main() {
   // Call
   writeln(getCol!0());
}

Thanks for your reply. It solved my problem.