On 11/30/2011 10:17 AM, Xinok wrote: > On 11/30/2011 11:46 AM, Steven Schveighoffer wrote: >> >> foreach(_i; ubyte.min..ubyte.max + 1){ >> ubyte i = cast(ubyte)_i; >> } >> >> But my point was, foreach over a range gives me all the elements in a >> range, regardless of how the underlying loop is constructed. The >> limitation by the compiler is artificial. >> >> I remember at one point there was someone who had actual code that >> resulted in a loop for ubytes, or was trying to figure out how to >> foreach over all possible ubyte values. >> >> -Steve > > It shouldn't be. The compiler should be smart enough to inline the > function and optimize the typecast to something like this: > > void main(string[] args){ > foreach(_i; ubyte.min .. ubyte.max + 1) > writeln(*cast(ubyte*)&_i); > } > > This would actually be faster since you don't have to iterate two > variables.
But variables may be implemented on CPU registers and be fast. That is easy to do with Steven's code and cast above. In your code, taking the address of _i would put _i in memory. (Unless the compiler has an optimization for that. Too lazy to check. :()
Also, *cast(ubyte*)&_i works only on little endian systems, right? But you meant that the compiler should take care of endianness as well.
Ali