What I am trying to do is to have a class which is aware of its offset within an enclosing class with no runtime overhead at all. Here's an example of what I wish I could do:
template<int offset>
struct Inner {
};
struct Outer {
int placeholder;
Inner<offsetof(Outer, ptr)> ptr;
};
The above code doesn't compile because offsetof(Outer, ptr) doesn't know about ptr (it's helping define it). I have implemented a few versions of this same idea that do incur runtime overheads (both in memory and executed instructions), but I'm having trouble implementing a "0 runtime overhead" version like my dream implementation above. Any ideas how this can be done?
It could be simulated ugly way with the code duplication and a lot of boilerplate asserts as: declare a 'template' class with the same layout, declare target class, do a compile time assert that corresponding fields have the same offsets.