How do I iterate over enum members at runtime?

Andrej Mitrovic none at none.none
Sat Apr 9 13:20:06 PDT 2011


E.g.:

enum Metrics : int 
{
	SM_CXSCREEN = 0,
	SM_CYSCREEN,
	SM_CXVSCROLL,
	SM_CYHSCROLL,
	SM_CYCAPTION,
	SM_CXBORDER,
}

void foo(int m)
{
}

void main()
{
    foreach (m; Metrics)
    {
        foo(m);
    }
}

This won't work.

I know there's traits to get strings at compile time, e.g.:
auto b = [ __traits(allMembers, Metrics) ];

but this doesn't help me try out those enum values at runtime. It could almost work if I could use a mixin() in a foreach loop, but that's lucid dreaming.

Another alternative might be to create an array out of the enum, but I don't know of any way of doing this.


More information about the Digitalmars-d-learn mailing list