Whats the best way to get a struct/class member type?

Simen Kjærås simen.kjaras at gmail.com
Sun Apr 1 15:04:58 PDT 2012


On Sat, 31 Mar 2012 15:20:42 +0200, simendsjo <simendsjo at gmail.com> wrote:

> Seems __traits doesn't have a __traits(getMemberType, T, name).
> Now I'm doing the following:
> T t; // instance to use in getMember
> alias typeof( __traits(getMember, t, name) ) MemberType;
>
> Is this the only way to get the type of a field based on the name?

I'd think so, apart from tupleof, as Jacob's already mentioned.

It's easily factored out, though:

template getMemberType(T, string name) if (is(typeof(__traits(getMember,  
T, name)))) {
     alias typeof(__traits(getMember, T, name)) getMemberType;
}


More information about the Digitalmars-d-learn mailing list