I have structs like
struct RGBA (T) {/* ... */}
struct BMPFile (DataT) if (is(DataT == RGBA)) {/* ... */}
But is(DataT == RGBA)
cannot work because DataT is a type and RGBA is a template. Instead I need check if a type is an instantiation of a template in order to declare file
like
BMPFile!(RGBA!ushort) file;
In a comment @FeepingCreature showed
struct RGBA(T) {
alias void isRGBAStruct;
}
struct BMPFile (DataT) if (is(DataT.isRGBAStruct)) {}
Although to be working I have no tips on alias void isRGBAStruct
, that seems a hack. Hopefully std.traits will cover this.
I think you're trying to be over-specific. The way I would handle this is to allow any type for DataT
, so long as it implements whatever interface I need.
The way this is done in the D2 standard library, I believe, is to have templates like IsIntegral
that test various properties of a type.
For example, let's say your requirement for DataT
is that you can finangle
it. You might write:
template IsAppropriate(DataT)
{
enum IsAppropriate = is(typeof( { DataT d; d.finangle(); }() ));
}
Take the above with a grain of salt: I haven't tested it, but I believe that's the basic pattern. If you aren't familiar with the above, what it's doing is checking to see if the given anonymous function compiles; it can only compile if it's possible to finangle
the DataT
.
This is all from memory so take it with a bit of salt.
First, those structs need bodies, you should be getting parse errors from that. Second, the template constraint on BMPFile
won't work because it is attempting to compare RGBA!ushort
(a type) and RGBA
(a template) which are different kinds of things. Last I checked (and it's been a while) there is no clean way to check if a type is an instantiation of a given template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With