I am in the following situation:
//This is Public
class B{/*usefull stuff*/};
B*f();
void g(B*b)();
//Those classes are only declared the translation unit of f and g.
class Whatever1{/*Implementation details only useful to f and g*/};
class Whatever2{/*Implementation details only useful to f and g*/};
class A{
public:
Whatever1 w1;
Whatever2 w2;
B b;
};
In function g, I want to convert the parameter (a pointer to B) to a pointer to A.
B instances are always wrapped in A instances.
I ended up with this:
ptrdiff_t lag(){
A a;
return (char*)&a.b-(char*)&a;}
void g(B*b){
A*a=(A*)((char*)b-lag());
//Work with a
}
This solution makes me very unconfortable.
Is it 100% correct and portable to do offset computations like this ?
Does it triggers Undefined Behavior in any way ?
Edit: std::is_standard_layout< A >::value is 1.