What is the preferred type for loop indexes when using foreach
in D, int
, uint
or simply automatic by omitting the type?
In general, indices should be size_t
. The same with length
. You're going to have issues with 32-bit vs 64-bit machines if you try and use int
or uint
. size_t
is what the language uses for array indices and length
. It's aliased to uint
on 32-bit machines and ulong
on 64-bit machines.
So, if you're going to give an index a type, give it size_t
. However, the type will be inferred to be size_t
by foreach
when iterating over an array. So, in most cases, there's no reason to list the type.