digitalmars.D.learn - Template instance does not match template declaration
- Marc (20/20) Aug 15 Hello,
- 0xEAB (11/12) Aug 15 It seems like you’ve accidentally used the wrong syntax for the
- 0xEAB (31/33) Aug 15 With the syntax from your code snippet you’ve put a constraint on
- Marc (2/14) Aug 15 Thanks. It work now.
- Sergey (12/32) Aug 15 D doesn't use ":" for types
- Marc (2/38) Aug 15 Thanks for your reply. It solved my problem.
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
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
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
On Friday, 15 August 2025 at 12:18:30 UTC, 0xEAB wrote:On Friday, 15 August 2025 at 12:02:19 UTC, Marc wrote:Thanks. It work now.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
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
On Friday, 15 August 2025 at 12:20:16 UTC, Sergey wrote:On Friday, 15 August 2025 at 12:02:19 UTC, Marc wrote:Thanks for your reply. It solved my problem.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