Is there a way to slice non-array type in @safe?

Paul Backus snarwin at gmail.com
Thu Jul 11 18:46:57 UTC 2019


On Thursday, 11 July 2019 at 16:31:58 UTC, Stefanos Baziotis 
wrote:
> I searched the forum but did not find something.
>
> I want to do this:
>
> int foo(T)(ref T s1, ref T s2)
> {
>     const byte[] s1b = (cast(const(byte)*)&s1)[0 .. T.sizeof];
>     const byte[] s2b = (cast(const(byte)*)&s2)[0 .. T.sizeof];
> }
>
> Which is to create a byte array from the bytes of the value 
> given, no matter
> the type. The above works, but it's not @safe.
>
> Thanks,
> Stefanos

Casting from one type of pointer to another and slicing a pointer 
are both @system, by design. What's the actual problem you're 
trying to solve? There may be a different way to do it that's 
@safe.


More information about the Digitalmars-d-learn mailing list