Challenge

Manu via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 3 18:08:39 PDT 2016


On 4 October 2016 at 00:25, John Colvin via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> On Monday, 3 October 2016 at 13:19:19 UTC, Manu 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...
>
>
> template isStaticMember(T, string member)
> {
>     mixin(`alias mem = T.` ~ member ~ `;`);
>     import std.traits : FunctionTypeOf;
>     static if (is(FunctionTypeOf!mem == function) &&
> is(FunctionTypeOf!(typeof(&mem)) == function))
>         enum bool isStaticMember = __traits(isStaticFunction, mem);
>     else
>         enum bool isStaticMember = is(typeof(&mem));
> }
>
> Basically, using FunctionTypeOf catches @property functions (which just
> typeof wouldn't), but it also catches member variables with function types,
> so we need the second FunctionTypeOf to see if it's still a function when
> you take its address (true for member functions, including @property
> functions, not true for member variables with function types).
>
> Everything else is just "can you take the address of this".

Very nice. This is the quality of solution I was looking for! 3 Significant LOC.
I knew a simple solution must exist. I didn't think of FunctionTypeOf.


More information about the Digitalmars-d mailing list