简体   繁体   中英

Statically get offset of member variable

I'm searching a way to get the offset of a member variable to statically pass this offset to the member variable. Basically I want achieve this:

template <std::intptr_t OFFSET>
struct A
{
    std::intptr_t self()
    {
        return reinterpret_cast<std::intptr_t>(this) - OFFSET;
    }
};
struct B
{
    int some_variables[256];
    A<???> a;
};
int main()
{
    B b;
    assert(reinterpret_cast<std::intptr_t>(&b) == b.a.self()); // shall pass
    return 0;
}

Is there a way to do this?

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:

  1. Since offsetof(type, member) is well-defined only for standard-layout type s, the enclosing type must be standard-layout,
  2. 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; .

I had an idea and eventually found a way to statically obtain the address of the member:

template <class T, class R, R T::* MEMBER>
struct B
{
    std::intptr_t self()
    {
        // got this reinterpret_cast from https://stackoverflow.com/a/5617632/4043866
        std::intptr_t offset = reinterpret_cast<std::intptr_t>(&(reinterpret_cast<T const volatile*>(NULL)->*MEMBER));
        std::intptr_t _this = reinterpret_cast<std::intptr_t>(this);
        return _this - offset - sizeof(R);
    }
};
template <class T, class... BASES>
constexpr std::size_t acc_sizes()
{
    return sizeof(T) + acc_sizes<BASES...>();
}
template <class... BASES>
struct B_START
{
    std::intptr_t self()
    {
        return reinterpret_cast<std::intptr_t>(this) - acc_sizes<BASES...>();
    }
};
struct Z
{
    int a;
};
#pragma pack(1)
struct A
    : Z
{
    B_START<Z> a;
    B<A, B_START<Z>, &A::a> b;
    B<A, B<A, B_START<Z>, &A::a>, &A::b> c;
};

Now I can write:

A a;
a.b.self();

To address the correct function, without any lose of memory space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM