Templated structs / variant values

Marek Janukowicz marek at janukowicz.net
Mon Aug 19 16:51:57 PDT 2013


H. S. Teoh wrote:

> On Tue, Aug 20, 2013 at 01:09:16AM +0200, Marek Janukowicz wrote:
>> Jacob Carlborg wrote:
> [...]
>> > In "settings" you should be able to:
>> > 
>> > 1. Iterate over all fields of the type Setting using
>> > __tratis(derivedMembers)
>> 
>> How do I do that? My understanding of types in case of templates is
>> really poor... If I do something like:
>> 
>>     foreach( s; __traits(derivedMembers, typeof(this))) {
>> 
>> how do I go from "s" into type of this member (especially that a type
>> is an instantiated struct template?).
> 
> To get the type:
> 
> alias type = typeof(__traits(getMember, this, s));
> 
> To get the value:
> auto value = __traits(getMember, this, s);
> 
> Or you can get both:
> 
> auto value = __traits(getMember, this, s);
> alias type = typeof(value);
> 
> 
>> And more generally - how do I check the type of a variable?
> 
> int x;
> assert(is(typeof(x) == int));

Thanks, but how do I get the list of members that are of instantiated 
Setting struct type? If I do it like this:

alias type = typeof(__traits(getMember, this, s));
      if (is ( type == Setting))

I get:

Error: struct aa.Setting(T, string desc, T deflt) is used as a type

-- 
Marek Janukowicz


More information about the Digitalmars-d-learn mailing list