is a pointer to data member its offset?

954 views Asked by At

Am I safe to assume that the offset of a data member (offsetof(mystruct, myfield)) is numerically equal to the raw value of a member pointer retrieved with &mystruct::myfield, or is it implementation dependent?

2

There are 2 answers

0
Mike Seymour On BEST ANSWER

No; the implementation of a pointer-to-member is not specified, and there is no defined conversion to get the "raw" value.

0
AnT stands with Russia On

Formally, it is implementation-dependent, of course. In real life, yes, the most popular implementation of pointer-to-data-member pointers is (or is based on) plan-and-simple offset, i.e. the same thing that offsetof evaluates to.

Some implementations use the exact offset, resorting to 0xFFF... pattern to represent the null-pointer value. Some implementatiuons reserve 0x000... pattern for null-pointer value, while incrementing all "non-null" offsets by 1.