struct to byte[]

Luís Marques luismarques+spam at gmail.com
Wed Dec 13 01:51:28 PST 2006


John Demme wrote:
> The reason Foo cannot be casted to ubyte is because they are different
> sizes.  ubyte[] is a pointer and a length, whereas Foo is anything.  It
> makes more sense to be able to be able to cast from a Foo* to ubyte, thus
> the & in Kirk's code.  This, however, will only fill the pointer part of
> ubyte[]- it still can't know the length.  The [0 .. Foo.sizeof] bit gives
> the dynamic array the length.
> 
> If you're looking for an operator to convert a arbitrary piece of data to an
> array of bytes, a cast is not what you're looking for.  I agree this would
> be useful, though... A template in phobos would be nice.

Foo, being a struct, has an address and a .sizeof, giving us a pointer 
and a length. Why then type it explicitly, when the compiler has all the 
information and the cast is explicit, unambiguous and readable?



More information about the Digitalmars-d mailing list