Iterating through enumeration

Alexandr Druzhinin drug2004 at bk.ru
Sun Aug 12 04:19:12 PDT 2012


12.08.2012 17:45, Jonathan M Davis пишет:
> 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
>
didn't know about this module, definitly I'll take a look at it, thanks!


More information about the Digitalmars-d-learn mailing list