unsafeBitCast() and alignment

unsafeBitCast is a value operation in Swift, so there's no fundamental reason it needs to require the alignment to match, and I would call it a bug if we do. We can copy the value to a correctly-aligned memory buffer (if it even needs to hit memory at all). reinterpret_cast in C++ casts a pointer to one type into one to another, and that's where the alignment requirement comes from. The closer analogy in C++ would be C++23's std::bit_cast function, which only requires the sizes to match, not alignment since it is defined in terms of memcpy.

7 Likes