Base type of a typedef

bearophile bearophileHUGS at lycos.com
Fri Dec 21 18:15:30 PST 2007


Thank you very much Daniel Keep, this is the code I use now:

/*************************************
Template, gives the base type of type type-defined one or more times.

Example:
----------------------
typedef int T;
typedef T T2;
BaseTypedef!(T2) ==> int
----------------------
*/
template BaseTypedef(T) {
    static if( is( T BaseType == typedef ) )
        alias BaseTypedef!(BaseType) BaseTypedef;
    else
        alias T BaseTypedef;
}

unittest { // Tests of BaseTypedef!()
    assert( is(BaseTypedef!(int) == int) );

    typedef int T1;
    assert( is(BaseTypedef!(T1) == int) );

    typedef T1 T2;
    assert( is(BaseTypedef!(T2) == int) );

    typedef T2 T3;
    assert( is(BaseTypedef!(T3) == int) );

    struct S { string a; }
    assert( is(BaseTypedef!(S) == S) );

    typedef S S2;
    assert( is(BaseTypedef!(S2) == S) );
} // End tests of BaseTypedef!()


And it solves my problem nicely! :o)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list