Trait for "can be instantiated"?
Ben Jones
fake at fake.fake
Mon May 9 21:24:38 UTC 2022
I have a struct template that takes an alias parameter and I'm
trying to distinguish between type parameters and enum values.
std.traits.isType works for this except for one edge case:
```
import std.traits;
import std.stdio;
struct S{}
enum x;
enum y = 5;
struct Wrap(alias T) {
static if(isType!T){
T value; //When t == x this doesn't work because `x` is
opaque and has no default initializer
}
}
void main()
{
pragma(msg, isType!x); //true
pragma(msg, isType!y); //false
Wrap!S ws; //OK
Wrap!x wx; //error instantiating
Wrap!y wy; //OK, because we the static if condition is false
}
```
x is a "type" as far as isType is concerned (which makes sense to
me), but I don't think you can ever declare a variable of that
type since there's no way to initialize it... Is there a trait
that can tell if you can initialize a variable of a certain type?
The best I can think of is __traits(compiles, "T x;"), but it
seems like it should be possible to do better?
More information about the Digitalmars-d-learn
mailing list