How can I match every instance of a template type (struct)?
rempas
rempas at tutanota.com
Tue Jul 12 13:34:42 UTC 2022
I want to do something like the following:
```d
import core.stdc.stdio;
Test!(T, "mode1") make_test(T)(T data) {
Test!(T, "mode1") t = { data };
return t;
}
struct Test(T, string mode = "ref") { T data; }
extern (C) void main() {
auto obj = make_test(20);
static if (is(typeof(obj) == Test)) { printf("YES!!!!!!!\n"); }
}
```
So, I just want to be able check if a variable is a given struct
type. I also want to be able to do something similar but having a
(templated) function that returns a struct type without having to
be limited to a specific initialization of it.
Obviously, the given code will not work. It will result to the
following error message:
```
Error: template struct `test.Test(T, string mode = "ref")` is
used as a type without instantiation; to instantiate it use
`Test!(arguments)`
```
Any ideas? Also, like in every question I make, the solution must
be "betterC" compatible.
**CLARIFICATION**
I have a bad feeling that the post is not clear enough, so I'll
save us all some time by making it clear.
I know I can do this:
```d
static if (is(typeof(obj) == Test!(int, "mode1"))) {
printf("YES!!!!!!!\n"); }
```
And it will work but this is not what I want. I want to much
EVERY "Test" type regardless of what's the value
of its templated arguments.
More information about the Digitalmars-d-learn
mailing list