Suppose there's a structure
struct Thing {
int a;
bool b;
};
and I get a pointer to member b
of that structure, say as parameter of some function:
void some_function (bool * ptr) {
Thing * thing = /* ?? */;
}
How do I get a pointer to the containing object? Most importantly: Without violating some rule in the standard, that is I want standard defined behaviour, not undefined nor implementation defined behaviour.
As side note: I know that this circumvents type safety.
Thing
pointer from abool
address. Why don't you pass the Thing address/pointer to your function?offsetof
can be undefined. You’ve probably moved on from this since November 2015, but if it’s still relevant and a non-offsetof
answer turns up, I suggest you implement it. Fortunately, for an allocator, you don’t need to return a pointer. You can return any pointer-like structure which satisfiesNullablePointer
andRandomAccessIterator
andtypedef
it topointer
.