You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While it might be up to taste, I personally like the immediacy and self-explanatory nature of @bitCast - going an extra step via &.* hides the intention, but std.bitCast of course works as well.
I'm slightly concerned however that, if @ptrCast and bit cast are so similar in nature, and there is no more lingual difference (both use @ptrCast), one could easily mismatch the pointer levels with multiple layers of indirection:
fnfoo(a: *[*]u32){
varb=@ptrCast([*]i32, a); //either meant *[*]i32, or forgot to dereference a
}
I only now realized that status-quo Zig lets this pass without an error.
Maybe (if the note on copy elision optimization were to be resolved) @ptrCast works as the underlying primitive, and std should have both ptrCast, which explicitly checks that the pointer levels match, and bitCast, which explicitly checks you're not casting between pointers? (That way you have to decide to use std.ptrCast to opt in to checks, or @ptrCast to opt out.)
In that case I would also suggest putting a reference to std.ptrCast and std.bitCast into the documentation of @ptrCast - especially since the language doc is currently the only doc around.
Now that it's certain the Zig specification (#75) will have a well-defined memory model, it's perfectly legal for userland code to do this:
One reason perhaps to not do this, is that with copy elision (#287),
@bitCast
can participate in a no-copy expression, like this:With copy-elision, the initialization of x directly initializes a
Bar
, without a step in between. Contrasted with a userland function:Now the
Foo
initialization operates on stack memory which is then passed to the userlandbitCast
function, and the result is memory-loaded intox
.To achieve the same semantics in userland, one would have to do this:
The text was updated successfully, but these errors were encountered: