Anyone know what's going on here? (variable with an instantiated template as a type shows up as void when instantiating another template)

Philippe Sigaud philippe.sigaud at gmail.com
Fri Apr 23 13:53:04 PDT 2010


On Fri, Apr 23, 2010 at 14:57, Robert Jacques <sandford at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100423/eac3fdd6/attachment.html>


More information about the Digitalmars-d mailing list