Test for self referential templated Class

Fawzi Mohamed fmohamed at mac.com
Wed Jul 2 02:49:48 PDT 2008


On 2008-06-30 21:53:40 +0200, "Manfred_Nowak" <svv1999 at hotmail.com> said:

> Michel Fortin wrote:
> 
>> You could add a dummy member in C!(T) and check for its presence.
> 
> Yes. That seems to be less hackish than what Robert suggested. But
> still no possibility to fetch a non trivial depth of recursion.

I simply use two templates, one to give me the depth of recursion, one 
to give the base type...

This is for arrays, but it can be adapted also for other types

/// Strips the []'s off of a type.
template arrayBaseT(T)
{
    static if( is( T S : S[]) ) {
        alias arrayBaseT!(S)  arrayBaseT;
    }
    else {
        alias T arrayBaseT;
    }
}

/// Count the []'s on an array type
template arrayRank(T) {
    static if(is(T S : S[])) {
        const uint arrayRank = 1 + arrayRank!(S);
    } else {
        const uint arrayRank = 0;
    }
}

Fawzi




More information about the Digitalmars-d mailing list