Templates and stringof...

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Aug 3 15:16:10 PDT 2012


On 8/3/12, Era Scarecrow <rtcvb32 at yahoo.com> wrote:
>   Now, how do I get the template's stringof to print out 'i_num.i'
> and not 'i'?

Courtesy of Philippe Sigaud, slightly edited to my style:

/**
    Return the fully qualified name of a symbol.
    Implemented by Philippe Sigaud in the D Templates book.
    See https://github.com/PhilippeSigaud/D-templates-tutorial
*/
template ScopedName(alias a)
{
    // does it have a parent?
    static if (__traits(compiles, __traits(parent, a)))
    {
        // If yes, get the name and recurse
        enum ScopedName = ScopedName!(__traits (parent, a)) ~ "." ~ NameOf!(a);
    }
    else
    {
        // if not, it’s a module name. Stop there.
        enum ScopedName = NameOf!a;
    }
}

template NameOf(alias a)
{
    enum string NameOf = __traits(identifier, a);
}

template XYZ(alias x)
{
    enum XYZ = ScopedName!x ~ "=100;";
}


More information about the Digitalmars-d-learn mailing list