The is expression
Philippe Sigaud
philippe.sigaud at gmail.com
Sun Apr 3 07:11:48 PDT 2011
On Sat, Apr 2, 2011 at 13:05, enuhtac <enuhtac_lists at gmx.de> wrote:
> This is the type I would like to check for:
>
> struct A( T, string s )
> { ... };
Hi,
the trick is to use a function do the work for you. Let's define isAnA:
void isAnA(T, string s)( A!(T,s) a) { }
isAnA can only be called (compiled) with your A.
A!(int, "abc") a;
A!(double, "") b;
isAnA(a); // OK
isAnA(b); // OK
isAnA(123); // does not compile.
So you can nest it in a template and check at compile-time if it compiles:
template isMyA(Type)
{
static if (__traits(compiles,
{
void isAnA(T, string s)(A!(T,s) a) {}
isAnA(Type.init); // create a value of
type Type, see if isAnA accepts it.
}))
enum bool isMyA = true;
else
enum bool isMyA = false;
}
Note that this is strictly tailored to A's with (T, string s) as
arguments. You can relax the constraints by adapting the test
function.
For a more generic way to test for this, you can have a look there:
http://svn.dsource.org/projects/dranges/trunk/dranges/docs/templates.html
("isInstanceOf" and "Template ParametersTypeTuple")
http://svn.dsource.org/projects/dranges/trunk/dranges/docs/typepattern.html
(look for "isA")
Philippe
More information about the Digitalmars-d-learn
mailing list