Statically get offset of member variable
First of, as requested, you're goal is not achievable as the type of a
impacts the offst of a
inside B
:
struct B
{
int some_variables[256];
A</* offset of a inside B */> a;
};
This is alignment.
You could use the standard macro offsetof
. This implies two things:
- Since
offsetof(type, member)
is well-defined only for standard-layouttype
s, the enclosing type must be standard-layout, - and since
offsetof
can only be "called" on complete types, its statically-computed result can only be set to the subobject dynamically; it canoot be a template non-type parameter, but can be a constructor argument.
Full program
#include <cassert>
#include <cstdint>
#include <cstddef>
struct Location
{
Location(std::size_t offset) : offset_(offset) {}
std::size_t offset_;
operator std::intptr_t () const { return reinterpret_cast<std::intptr_t>(this) - offset_; }
};
struct SomeType
{
int some_variables[256];
Location location = offsetof(SomeType, location);
};
int main()
{
SomeType obj;
assert(reinterpret_cast<std::intptr_t>(&obj) == obj.location); // does pass
}
live demo
But as you commented, this is quite useless as Location
could be simply defined as
template<class T>
struct Location
{
Location(T* location) : location_(location) {}
T* location_;
operator T* () const { return location; }
};
and initialized with Location location = this;
.