Let’s define a property POD (plain old data), meaning that the data is a contiguous block in memory and all bit-patterns of the data are valid.
i128have this property.
f64do I think, but I’m not 100% sure.
- raw pointers (
*const) I don’t really have a clue. My intuition says no, since although they are conceptually unsigned ints, is it guaranteed that they are in that format? Maybe we have to treat these as opaque.
structs - I feel like these should be if all their fields are. Even if there is padding, you don’t care what’s in it anyway.
enums - These can’t be unless you use a u8 discriminant and have exactly 256 variants, for example, so best just assume they are not.
- zero-sized types and
!- I’d say these are since they have 1, 0 representations respectively, but it’s probably academic.
- other pointers - I assume these aren’t since raw pointers aren’t.
unions - Yes if all variants have this property
- any other types I haven’t thought of (I’m just avoiding trait objects since they can’t be)
I want to understand this property since it is important for writing unsafe code, and understanding the behavior of
unions. Also, it seems that the compiler can assume that invalid bit patterns never exist - meaning that you can get weird UB (that might randomly appear in a later llvm) if you do things like take references to it, even if those references are never read. Is this true? I don’t feel like I really understand.