2

I'm trying to implement a hash function that can be used at compile-time. To achieve this, I need to reinterpret a value (e.g., an int or a float) as its raw bytes.
However, when using compile-time evaluation, pointer type punning (reinterpretation) is not allowed due to strict aliasing and other restrictions.

Here's a simplified example to illustrate the issue:

template <typename T>
constexpr size_t hash(const T& value) {
    const unsigned char* byte_ptr = reinterpret_cast<const unsigned char*>(&value); // NOT allowed.

    size_t hash = 0;
    for (size_t i = 0; i < sizeof(T); ++i) {
        hash ^= byte_ptr[i];
    }
    return hash;
}

Is there a standard-compliant way to reinterpret a value as bytes in a constexpr context?

2
  • 1
    Which version of the Standard are you using? C++20/23/26? Commented Dec 1, 2024 at 10:27
  • 6
    std::bit_cast? Commented Dec 1, 2024 at 10:32

1 Answer 1

5

std::bit_cast (available since C++20) supports constexpr.
You can bit_cast to std::array and then use std::array::operator[] (which supports constexpr as well) to access the bytes:

#include <array>
#include <cstddef>
#include <bit>

template <typename T>
constexpr size_t byte_sum(const T& value) {
    //const unsigned char* byte_ptr = reinterpret_cast<const unsigned char*>(&value); // NOT allowed.
    auto arr = std::bit_cast<std::array<unsigned char, sizeof(value)>>(value);
    size_t hash = 0;
    for (size_t i = 0; i < sizeof(T); ++i) {
        hash ^= arr[i];
    }
    return hash;
}

int main() {
    constexpr double d = 5;
    [[maybe_unused]] constexpr size_t h = byte_sum(d);
}

Live demo

Sign up to request clarification or add additional context in comments.

7 Comments

why use data() instead of [] ?
@463035818_is_not_an_ai you are right, it's simpler - updated.
How can you tell when it's a valid cast?
@DavidG what do you mean ?
... In general it should work for casting between trivially copyable types.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.