Whats the best way to get a struct/class member type?
Simen Kjaeraas
simen.kjaras at gmail.com
Mon Apr 2 01:51:35 PDT 2012
On Mon, 02 Apr 2012 09:58:18 +0200, simendsjo <simendsjo at gmail.com> wrote:
> On Mon, 02 Apr 2012 00:04:58 +0200, Simen Kjærås
> <simen.kjaras at gmail.com> wrote:
>
>> 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;
>> }
>
>
> getMember requires an instance, not a type, so it should be
>
> template getMemberType(T, string name) if(__traits(hasMember, T, name) {
> T t;
> alias typeof(__traits(getMember, t, name)) getMemberType;
> }
Tested and works for me under 2.058 with just the type.
More information about the Digitalmars-d-learn
mailing list