I am currently working on programming a pool allocator. My question boils down to the following code:
template <typename T>
union myUnion {
T data;
myUnion<T>* nextUnion;
};
void someFunction(){
myUnion<T> mu;
T* t = new (std::addressof(mu.data)) T();
//some code
myUnion<T>* mu2 = reinterpret_cast<myUnion<T>*>(t);
}
Is the address of mu always the same as mu2?
Yes.
9.2/19 (12.2/24 in N4659):
If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member.
If the union itself is standard-layout, then the address of the union is the same as its members'.
The addresses of the members are all the same, thanks to 9.5/1 (12.3/2 in N4659):
Each non-static data member is allocated as if it were the sole member of a struct. All non-static data members of a union object have the same address.