Error: template instance does not match template declaration

Tejas notrealemail at gmail.com
Wed Nov 17 13:24:22 UTC 2021


On Wednesday, 17 November 2021 at 12:55:15 UTC, Vitalii wrote:
> Thank you for response, Tejas!
>
> What I intended to do was make a class with only two states 
> ("Inspect" - do some analysis, "Execute" - do some stuff). 
> That's why I tried to use template specialization.
>
> The following code compiles successfully, but return *"fun with 
> unknown"* instead of *"fun with Mode1"*, as I supposed. I think 
> I missed some "static" thing in my code, but can't find any 
> working examples. Any ideas?
>
> ```
> import std.stdio : writeln;
>
> enum Mode1;
> enum Mode2;
>
> class Worker(Mode) {
> 	this() {}
>
> 	void fun() {
> 		static if (is(typeof(Mode) == Mode1)) {
> 			writeln("fun with Mode1");
> 		}
> 		else static if (is(typeof(Mode) == Mode2)) {
> 			writeln("fun with Mode2");
> 		}
> 		else {
> 			writeln("fun with unknown");
> 		}
> 	}
> }
>
> void main() {
> 	auto W = new Worker!(Mode1)();
> 	W.fun();
> }
> ```

You're not using a normal template here, you're using a template 
that takes non-type parameters(basically you're using a template 
that takes __variables__ as parameters rather than __types__)

```d
import std.stdio : writeln;

enum Mode{
     Mode1,
     Mode2
}
//enum Mode2; You're creating two completely different enum 
types, why??? This a mistake or intentional?

class Worker(Mode m) {
	this() {}

	void fun() {
		static if (m == Mode.Mode1) /*You can't write Mode1 directly, 
use enumType.enumValue :(*/{
			writeln("fun with Mode1");
		}
		else static if (m == Mode.Mode2) {
			writeln("fun with Mode2");
		}
		else {
			writeln("fun with unknown");
		}
	}
}

void main() {
	auto W = new Worker!(Mode.Mode1)();
	W.fun();
}
```


More information about the Digitalmars-d-learn mailing list