NovelRT icon indicating copy to clipboard operation
NovelRT copied to clipboard

Replace reinterpret_cast with `NovelRT::Utilities::Misc::BitCast` where appropriate

Open Pheubel opened this issue 1 year ago • 0 comments

With the addition of NovelRT::Utilities::Misc::BitCast type punning can be expressed in a more easy to understand way. It has been used in a few places so far, but might be able to be used in more places to better express what is going on.

Something to watch out for is that when bitcasting an instance through pointers, such as this, you should take care and see if you should de-reference a variable before passing it as an argument. As some types have the same size as pointers, you might end up transforming the pointer instead of it's value.

Example

Taking the following example:

inline bool operator==(GeoVector2F other) const noexcept
{
    return *reinterpret_cast<const glm::vec2*>(this) == *reinterpret_cast<const glm::vec2*>(&other);
}

Would become:

inline bool operator==(GeoVector2F other) const noexcept
{
    return NovelRT::Utilities::Misc::BitCast<glm::vec2>(*this) == NovelRT::Utilities::Misc::BitCast<glm::vec2>(other);
}

Pheubel avatar Nov 26 '22 17:11 Pheubel