Casting int to ubyte: element wise cast or slice cast
dkorpel at gmail.com
Fri Feb 15 18:11:11 UTC 2019
On Friday, 15 February 2019 at 17:18:28 UTC, H. S. Teoh wrote:
> Why? because that's the only case where you can reinterpret the
> .sizeof*.length bytes as an array of a different type. There
> is no conversion, the cast works as a reinterpretation. It's
> by design.
But my problem is that it *isn't* reinterpreted in the case of an
enum / array literal, the cast actually transcribes similar to
your .map example. Look carefully at the difference of case A and
B in my example code: the only difference is whether the int
array uses auto or enum.
I'm not going from ubyte to int, I'm going from int to ubyte
which should always be possible by multiplying the length by 4,
in my case:
int.sizeof * 2 == ubyte.sizeof * 8 == 8
This happens in case B of my example code, but not in case A.
There the cast goes from 4*2 bytes to 1*2 instead of 1*8 and I
don't get a runtime error.
More information about the Digitalmars-d-learn