Reputation: 3461
I am writing a C++ tagged pointer. I have a question about whether the operations I use to implement its basic functionality cause undefined behavior:
T*
, I reinterpret_cast
the pointer to a uintptr_t
. This allows me to perform bitwise operations on the memory address, which are necessary to add/extract/inspect the tag.reinterpret_cast
to cast the uintptr_t
to a void*
(after removing the tag). If a pointer to a specific type is needed, I take that void*
and static_cast
it to T*
(assuming T
is indeed the type held within the tagged pointer).So, in summary, I am performing the following transformations on a T*
:
reinterpret_cast
ing the T*
to a uintptr_t
reinterpret_cast
ing that uintptr_t
to a void*
, and possibly static_cast
ing that void*
back to a T*
.Does any of this result in undefined behavior? Additionally, am I using reinterpret_cast
and static_cast
correctly, in their intended use cases?
Upvotes: 6
Views: 984