Shouldn't casting an object to void* be considered @safe?
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Sat Dec 14 17:25:25 UTC 2019
On Friday, 13 December 2019 at 08:05:53 UTC, Andrej Mitrovic
wrote:
> However, I don't see this cast as being unsafe. Casting a class
> object to a `void*` doesn't break the type system by itself.
> You cannot assign a `void*` to any other pointer type without
> an additional cast, and that additional cast would be the
> unsafe one. Additionally, you cannot reference a `void*`, so as
> far as I can see it's fairly safe to use in @safe code.
>
> Wouldn't it make sense to allow casting reference types to
> `void*` in @safe code? Are there edge-cases I haven't
> considered?
Surely where code has a cast to `void*` and then later back to
some other pointer type, its overall safety is dependent on what
happens at both ends. The safety of the cast from `void*` cannot
be validated by the developer without knowing how the cast _to_
`void*` was done.
Making a cast to `void*` unsafe is therefore an important push to
the developer to say, "You need to look at this and validate what
you're doing against how you use it later."
More information about the Digitalmars-d
mailing list