Skip to content

[clang-tidy] Check request: detect incorrect usage of std::bit_cast #106987

Closed
@carlosgalvezp

Description

@carlosgalvezp

std::bit_cast is often presented as the correct alternative to type punning in C++. However, we see people use it as a drop-in replacement to reinterpret_cast , which only obfuscates the code and still has UB:

float x{};
-int* y = reinterpret_cast<int*>(&x);
+int* y = std::bit_cast<int*>(&x);

This usage is incorrect. Instead, the code should do:

int y = std::bit_cast<int>(x);

Most likely all usages of std::bit_cast<PointerType> are incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions