| Newsgroups |
gmane.comp.lib.fox-toolkit.user |
| Message-ID |
<[email protected]> |
On 2022-10-25 08:50, Sander Jansen wrote:
> Perhaps something similar like:
> https://en.cppreference.com/w/cpp/types/aligned_storage
Actually, the source type shouldn't really matter [we do
assume its aligned to proper boundary].
Then we can implement:
template <typename TOTYPE>
static inline TOTYPE* alias_cast(void* ptr){
union UNI { FXuval src[1]; TOTYPE dst[1]; };
return reinterpret_cast<UNI*>(ptr)->dst;
}
template <typename TOTYPE>
static inline const TOTYPE* alias_cast(const void* ptr){
union UNI { FXuval src[1]; TOTYPE dst[1]; };
return reinterpret_cast<const UNI*>(ptr)->dst;
}
And now we can have a nice shut-up-the-compiler cast:
alias_cast<SPACE>(space)->handle=nullptr;
This looks a lot more straightforward.
Now, this is contingent on member src and member dst
having the same address as the start of UNI.
I think this is the case, the alignment of a struct or
union should be (at least) as big as the alignment of the
alignment of the largest item in it, otherwise arrays of
them will not be able to guarantee alignment.
If not, then one would have to:
reinterpret_cast<TOTYPE*>(
reinterpret_case<char*>(ptr)+
STRUCTOFFSET(UNI,dst) -
STRUCTOFFSET(UNI,src)
);
I don't think its necessary, and even if it is, it has the downside
that it'll probably trigger -Wcast-align, or somesuch.
Thoughts?
-- JVZ