Just got a chance to test the isInstanceOf template and it does (after some fiddling) provide a workaround for this bug. Thanks.

Philippe Sigaud wrote:
On Fri, Apr 23, 2010 at 14:57, Robert Jacques <sandf...@jhu.edu <mailto:sandf...@jhu.edu>> wrote:

    PS. You can get things like this to work using template constraints.

    For example, here's a horrible hack:
    void unary_op(T)(T value)
    if(T.stringof[0..3] == "A!(" && T.stringof[$-1..$] == ")"  ){}


Maybe a bit less hacky (?) using an is expression:

void unary_op(T)(T value) if (is(T TT == A!N, uint N)) {}


N does not exist inside the function, though. For that, you'd have to recreate it with a static if:

void unary_op(T)(T value) if (is(T TT == A!N, uint N))
{
static if (is(T TT == A!N, uint N)) // read this as "if T is a type like A!N, for some uint N"
        writeln(N);
}


I have a template that may be useful there:

/**
isInstanceOf!(Type, templateName) is true iff Type is an instantiation (is that the right term?) of templateName.
*/
template isInstanceOf(T, alias templ)
{
static if (T.stringof.length >= __traits(identifier, templ).length && T.stringof[0..__traits(identifier, templ).length] == __traits(identifier, templ))
        enum bool isInstanceOf = true;
    else
        enum bool isInstanceOf = false;
}

Example:

void unary_op2(T)(T value) if (isInstanceOf!(T, A))
{
// T is an A!(someParam)
}

If you want to extract the 'someParam' part, it's doable by using a CT function that extracts what's between the first '(' and the corresponding ')'. I think I have somewhere a template that alias itself to the corresponding typetuple:

TemplateParametersTypeTuple!(Temp!(int, double, char[])) => TypeTuple!(int,double, char[])

Is anyone interested by this one?

Philippe




Reply via email to