util/Cast: reverse operands in ContainerAttributeOffset()

The result shall be positive.
This commit is contained in:
Max Kellermann 2018-08-20 14:23:02 +02:00
parent edd9d16c84
commit 9d63c8220b

View File

@ -53,7 +53,7 @@ template<class C, class A>
static constexpr inline ptrdiff_t
ContainerAttributeOffset(const C *null_c, const A C::*p)
{
return ptrdiff_t((const char *)null_c - (const char *)&(null_c->*p));
return ptrdiff_t((const char *)&(null_c->*p) - (const char *)null_c);
}
template<class C, class A>
@ -70,7 +70,7 @@ template<class C, class A>
static inline constexpr C &
ContainerCast(A &a, const A C::*member)
{
return *OffsetCast<C, A>(&a, ContainerAttributeOffset<C, A>(member));
return *OffsetCast<C, A>(&a, -ContainerAttributeOffset<C, A>(member));
}
/**
@ -80,7 +80,7 @@ template<class C, class A>
static inline constexpr const C &
ContainerCast(const A &a, const A C::*member)
{
return *OffsetCast<const C, const A>(&a, ContainerAttributeOffset<C, A>(member));
return *OffsetCast<const C, const A>(&a, -ContainerAttributeOffset<C, A>(member));
}
#endif