On Sunday, August 12, 2012 17:36:21 Alexandr Druzhinin wrote:
> Hello all.
> I have some enum foo { first=1, second=2, fourth=4, sixth=6} and I'd
> like to go through this enumeration like it:
> foreach(m; foo.min..foo.max) {
> bar(m);
> }
> But without excluding foo.max and correct handle situation when enum
> identifiers aren't consequentive.
foreach(m; std.traits.EnumMembers!foo)
bar(m);
Though be aware that since EnumMembers results in a TypeTuple, the foreach is
actually done at compile time, meaning that you'd actually be getting
bar(foo.first);
bar(foo.second);
bar(foo.fourth);
bar(foo.sixth);
Normally, that doesn't matter, but if you try and do fancier stuff, the fact
that the foreach is executed at compile time could affect what you're doing,
and if want your function to be small (e.g. inlining), then that would cause
problems.
- Jonathan M Davis