,offsetof
实现为:
#define offsetof(s,m) ((size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
我的问题是:
为什么reinterpret_cast
?
为什么volatile
?
为什么size_t
,而不是ptrdiff_t
?
答案 0 :(得分:-2)
&reinterpret_cast<char const volatile&>(expr)
也是用于实现std::addressof
的代码。 std::addressof
是必要的,因为C ++允许重载一元 - &
运算符,但是当它被标记为constexpr
时,在C ++ 17之前不能使用实际的库函数。< / p>
size_t
而不是ptrdiff_t
,因为这是标准所说必须返回的内容。
编辑:虽然技术上标准没有直接指定这种情况,但实际上编译器由于其他逻辑和历史实践而被迫覆盖它。
答案 1 :(得分:-2)
@NgModule({ // ... imports: [ HttpClientModule, JwtModule.forRoot({ config: { tokenGetter: () => { return <myService>.getToken(); // Here } } }) ] }) export class AppModule {}
reinterpret_cast&lt;&gt;()是将指针转换为size_t的唯一方法
why reinterpret_cast?
volatile会阻止优化器优化表达式why volatile?
。
(((s*)0)->m))
why size_t, not ptrdiff_t?
返回一个偏移量,而不是两个指针之间的元素数。