Shouldn't casting an object to void* be considered @safe?
Dennis
dkorpel at gmail.com
Sat Dec 14 18:46:38 UTC 2019
On Saturday, 14 December 2019 at 17:54:20 UTC, Joseph Rushton
Wakeling wrote:
> What's the point in generating a pure object identity, or a set
> of objects, unless those objects are going to be used?
Whether something is useful or a good practice is not relevant
for @safe, it is only concerned with whether it makes memory
corruption possible.
> And how do you validate that that usage is safe without knowing
> something about the circumstances in which those pure
> identities were generated?
Any function that takes a void* and casts it to a different
pointer that gets dereferenced is not @safe, whether class
casting to void* is allowed or not. You simply can't assume
anything about a void* even in @safe code.
Note that casting pointer types to void is already allowed in
@safe:
```
class C {}
void main() @safe {
void* v0 = cast(void*) new int; // allowed, implicit
conversion
void* v1 = cast(void*) 0xDEAFBEEF; // not allowed, not a
pointer type
void* v2 = cast(void*) new C(); // not allowed, not a pointer
type
}
```
The spec says:
> - No casting from a pointer type to any type other than void*.
> - No casting from any non-pointer type to a pointer type.
https://dlang.org/spec/function.html#safe-functions
The second line could be changed to "No casting from any
non-pointer type to a pointer type other than void*" however.
More information about the Digitalmars-d
mailing list