struct to byte[]

Alexander Panek a.panek at brainsware.org
Wed Dec 13 03:12:30 PST 2006


ubyte [] toByteArray (T) (T t) {
	return (cast(ubyte *)&t)[0..T.sizeof].dup
}
> Is there a reason for this explicit cast not to work automatically? (I 
> know that templates can alleviate this)
> 
> E.g.
> 
> struct Foo
> {
>     int a;
>     float b;
> }

Foo f;
auto b = toByteArray!(Foo)(f); // Yay!


Hope that helps. :P

> 
> -- 
> Luís



More information about the Digitalmars-d mailing list