I am using this code to convert a pointer to a size_t, it is used in a ::std::hash function that should hash a given pointer at compile time, and since reinterpret_casts are not allowed in constexpr, I came up with the following solution, it works as expected but I want to know whether this can be considered a good idea. It would also be great if you could give me a few pointers on how portable this piece of code actually is.
union {
size_t datr;
void* pointer;
} dummy{.pointer = (void*) somePointer};
size_t pointerAsAsize_t = dummy.datr; // how portable is this?
Is there any better solution - as stated above, I want to create a ::std::hash that runs at compile time.
Thanks in advance, thejack
Aucun commentaire:
Enregistrer un commentaire