【发布时间】:2018-12-25 23:05:32
【问题描述】:
我遇到了以下 c++ 代码:
#define OFFSETOF_MEMBER(t, f) \
(reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u)) // NOLINT
其中 t 是一个类型,f 是一个字段名。我想知道为什么我们可以将整数 16 作为 reinterpret_cast 的参数。
【问题讨论】:
-
请注意,这是未定义的行为。 C++ 确实有
offset_of,这是每个实现都定义的宏;一些实现会这样定义它。 -
^ 宏是
offsetof,在非标准布局类上使用它是 UB
标签: c++ pointers reinterpret-cast offsetof