Challenge

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 3 08:38:22 PDT 2016


On Monday, October 03, 2016 23:19:19 Manu via Digitalmars-d wrote:
> Fill in the blank...
> I'm having a really hard time with this. I've made it work with a
> mountain of code, and I want to see what others come up with...

It's certainly possible that this misses something, but it passes all of
your test cases:

template isStaticMember(T, string member)
{
    static if(!__traits(hasMember, T, member))
        enum bool isStaticMember = false;
    else
    {
        import std.meta : AliasSeq;
        import std.traits : FunctionTypeOf;
        alias sym = AliasSeq!(__traits(getMember, T, member))[0];
        static if(__traits(isStaticFunction, sym))
            enum bool isStaticMember = true;
        else static if(is(FunctionTypeOf!sym == function) &&
                       is(FunctionTypeOf!(typeof(&sym)) == function))
        {
            enum bool isStaticMember = false;
        }
        else
        {
            enum bool isStaticMember = !__traits(compiles, sym.offsetof) &&
                                       !is(sym == enum) &&
                                       !is(sym == class) &&
                                       !is(sym == struct) &&
                                       __traits(compiles, &sym);
        }
    }
}

- Jonathan M Davis



More information about the Digitalmars-d mailing list