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:
- When constructing a tagged pointer given a pointer
T*, Ireinterpret_castthe pointer to auintptr_t. This allows me to perform bitwise operations on the memory address, which are necessary to add/extract/inspect the tag. - When the user requests the original pointer back, I use
reinterpret_castto cast theuintptr_tto avoid*(after removing the tag). If a pointer to a specific type is needed, I take thatvoid*andstatic_castit toT*(assumingTis indeed the type held within the tagged pointer).
So, in summary, I am performing the following transformations on a T*:
reinterpret_casting theT*to auintptr_t- Later,
reinterpret_casting thatuintptr_tto avoid*, and possiblystatic_casting thatvoid*back to aT*.
Does any of this result in undefined behavior? Additionally, am I using reinterpret_cast and static_cast correctly, in their intended use cases?