In the following code:
using Previous = std::atomic<void*>;
template<class T>
struct Element{
Previous previous;
T value;
}
Am I allowed to do pointer arithmetic to get Element<T>*
from T*
?
Like this:
template<class T>
Element<T>* getElement(T* value){
return static_cast<Element<T>*>(static_cast<void*>(reinterpret_cast<unsigned char *>(value) - sizeof(Previous)));
}
Where T
may be non-standard layout.
P.S. I clearly see that I cannot use offsetof for this. But, may be for this special case, pointer arithmetic will work?