I want to write a function that takes two types T, U such that sizeof(T)+sizeof(U)<=8 and gets a uint64_t by just reinterpreting their bytes one after the other. However this does not seem to work. I am certain there is a quicker and more elegant (and correct) way to do it but I have no clue. Any tips are greatly appreciated.
#include <cstdint>
#include <iostream>
#include <vector>
template <typename T, typename U>
constexpr auto hash8(T x, U y) {
static_assert(sizeof(T) + sizeof(U) <= 8);
uint64_t u = 0;
uint64_t v = 0;
auto px = (uint8_t*)&x;
auto py = (uint8_t*)&y;
for (auto i = 0; i < sizeof(T); ++i) {
u |= (uint64_t)px[i];
u <<= 8;
}
for (auto i = 0; i < sizeof(U); ++i) {
v |= (uint64_t)py[i];
v <<= 8;
}
return u << (sizeof(U) * 8) | v;
}
int main() {
std::cout << hash8(131, 0) << '\n';
std::cout << hash8(132, 0) << '\n';
std::cout << hash8(500, 0) << '\n';
}