This seems like a duplicate of this question:
Nested data member pointer - not possible?
The crux of the accepted answer says:
A pointer-to-member can only be formed by an expression of type &qualified_id, which is not your case
The C++ standard says, in paragraph 8.3.3:
Pointer to Members:
In a declaration T D where D has the form
nested-name-specifier * attribute-specifier-seqopt cv-qualifier-seqopt D1
and the nested-name-specifier denotes a class, and the type of the identifier in the declaration T D1 is
“derived-declarator-type-list T”, then the type of the identifier of D is “derived-declarator-type-list cv
qualifier- seq pointer to member of class nested-name-specifier of type T”.
The optional attribute-specifier-seq (7.6.1) appertains to the pointer-to-member.
It turns out, there's a way to do what you want, that seems to be working on my current compiler (apple llvm 5.0). However, it is very implementation-dependant, and I would definitely recommend not using this solution in any kind of production code:
//added a few data members to make it non-trivial
struct SubUnit {
double d;
int value;
};
struct Unit {
bool b;
char c;
SubUnit sub_unit;
};
intptr_t suOffset = offsetof(Unit, sub_unit);
intptr_t intOffset = offsetof(SubUnit, value);
intptr_t totalOffset = suOffset + intOffset;
// there is no way to convert this offset directly in a
// pointer-to-member AFAIK, so we have to trick a bit
int Unit::* pui = nullptr;
auto puiAddr = &pui;
intptr_t* puiAddrAsIntPtrPtr = reinterpret_cast<intptr_t*>(puiAddr);
*puiAddrAsIntPtrPtr = totalOffset;
//pui should now "point to the right offset"
//let's test it
Unit u;
u.sub_unit.value = 123456;
int val = u .* pui;
std::cout << "val: " << val << std::endl;