Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace reinterpret_cast with NovelRT::Utilities::Misc::BitCast where appropriate #543

Open
Pheubel opened this issue Nov 26, 2022 · 0 comments

Comments

@Pheubel
Copy link
Contributor

Pheubel commented Nov 26, 2022

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 Pheubel changed the title Replace reinterpret_cast with NovelRT::Utilities::Misc::BitCast where appropriate Replace reinterpret_cast with NovelRT::Utilities::Misc::BitCast where appropriate Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant