Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred foreach Index Type

Tags:

foreach

d

What is the preferred type for loop indexes when using foreach in D, int, uint or simply automatic by omitting the type?

like image 656
Nordlöw Avatar asked Jan 27 '26 23:01

Nordlöw


1 Answers

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.

like image 111
Jonathan M Davis Avatar answered Jan 29 '26 13:01

Jonathan M Davis