I've been looking at some AOSP code, and I came across this (from a 5.0+ branch, under art/runtime/base/macros.h)
#define OFFSETOF_MEMBER(t, f) \ (reinterpret_cast<const char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<const char*>(16)) // NOLINT This is used in calls such as:
MemberOffset(OFFSETOF_MEMBER(Class, dex_cache )); Where Class is a class and dex_cache is a member under that class. This returns the offset of the field and then is used as the constructor of a MemberOffset class.
My question is why on earth would you want to hardcode that "16" in there? Wouldn't it make more sense just take the address of the member and subtract the base address of the class?
16and not0is that0would be (statically provable)nullptrand therefore the compile could fail.std::declvalshould make this sort of trickery unnecessary. It's not actually implemented this way, but I imagine it could be close.declval(for obvious reasons) doesn't permit ODR-use of its return value, and comparison of memory addresses counts.