Templated structs / variant values

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Aug 19 16:31:25 PDT 2013


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));


T

-- 
MACINTOSH: Most Applications Crash, If Not, The Operating System Hangs


More information about the Digitalmars-d-learn mailing list