[英]Statically get offset of member variable
我正在寻找一种方法来获取成员变量的偏移量,以静态地将此偏移量传递给成员变量。 基本上我想实现这个:
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;
}
有没有办法做到这一点?
首先,根据要求,你的目标是不是类型实现的a
影响的offst a
内部B
:
struct B
{
int some_variables[256];
A</* offset of a inside B */> a;
};
这是一致的 。
您可以使用标准宏offsetof
。 这意味着两件事:
offsetof(type, member)
仅为标准布局 type
定义良好,因此封闭类型必须是标准布局, offsetof
只能在完整类型上“调用”,因此其静态计算结果只能动态设置为子对象; 它可以是模板非类型参数,但可以是构造函数参数。 #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
}
但正如你评论的那样,这很简单,因为Location
可以简单地定义为
template<class T>
struct Location
{
Location(T* location) : location_(location) {}
T* location_;
operator T* () const { return location; }
};
并用Location location = this;
初始化Location location = this;
。
我有了一个想法,并最终找到了一种静态获取成员地址的方法:
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;
};
现在我可以写:
A a;
a.b.self();
解决正确的功能,不会丢失任何内存空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.