On Monday, October 03, 2016 08:38:22 Jonathan M Davis via Digitalmars-d wrote:
> 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);
>         }
>     }
> }

Actually, it can be reduced even further:

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) &&
                                       __traits(compiles, &sym);
        }
    }
}

Checking for the address makes it unnecessary to check for enums, classes,
or structs.

- Jonathan M Davis

Reply via email to