I have some rather odd behavior in my D program that I've narrowed down to this:
import std.algorithm; import std.stdio; import std.traits; enum E { a, b, c }; struct S { E e; }; void main() { immutable(S)[] source = [ S(E.a), S(E.a), S(E.b) ]; foreach (e; EnumMembers!E) { size_t c = count!(x => x.e == e)(source); writeln(e, " -> ", c); } } I would expect the output of this program to be something along the lines of: a -> 2 b -> 1 c -> 0 But the actual result is: a -> 2 b -> 2 c -> 2 Curiously, changing the for loop to foreach (e; [ E.a, E.b, E.c ]) produces my expected output. Using foreach (e; [ EnumMembers!E ]) also produces my expected result, so clearly my use of the range from EnumMemebers is the problem here...I just don't know why. By moving the count call to a separate function: size_t counte(Range)(E e, Range src) { return count!(x => x.e == e)(src); } and changing c's initialization to size_t c = counte(e, source);, the program works as I would expect. I am clearly doing something wrong, but I have no idea what and would appreciate some insight. My compiler is DMD64 D Compiler v2.059 on Linux. begin 644 enum_delegate.d M(R$O=7-R+V)I;B]R9&UD"@II;7!O<G0@<W1D+F%L9V]R:71H;3L*:6UP;W)T M('-T9"YS=&1I;SL*:6UP;W)T('-T9"YT<F%I=',["@IE;G5M($4@>R!A+"!B M+"!C('T["@IS=')U8W0@4R![($4@93L@?3L*"G-I>F5?="!C;W5N=&4H4F%N M9V4I*$4@92P@4F%N9V4@<W)C*0I["B`@("!R971U<FX@8V]U;G0A*'@@/3X@ M>"YE(#T](&4I*'-R8RD["GT*"G9O:60@;6%I;B@I"GL*("`@(&EM;75T86)L M92A3*5M=('-O=7)C92`](%L@4RA%+F$I+"!3*$4N82DL(%,H12YB*2!=.PH@ M("`@9F]R96%C:"`H93L@16YU;4UE;6)E<G,A12D*("`@('L*("`@("`@("!V M97)S:6]N("AC;W5N=&4I"B`@("`@("`@("`@('-I>F5?="!C(#T@8V]U;G1E M*&4L('-O=7)C92D["B`@("`@("`@96QS90H@("`@("`@("`@("!S:7IE7W0@ M8R`](&-O=6YT(2AX(#T^('@N92`]/2!E*2AS;W5R8V4I.PH@("`@("`@('=R <:71E;&XH92P@(B`M/B`B+"!C*3L*("`@('T*?0`` ` end