cast: add alignment cast

This commit is contained in:
Danny Robson 2018-05-09 17:47:47 +10:00
parent 2926776c97
commit 06e29ed136

View File

@ -152,6 +152,21 @@ namespace util::cast {
CHECK_NOTHROW (dynamic_cast<T> (v));
return static_cast<T> (v);
}
///////////////////////////////////////////////////////////////////////////
/// cast a pointer from one type to another, asserting that the required
/// alignment of the destination type has been satisfied.
template <typename DstT, typename SrcT>
DstT*
alignment (SrcT *src)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
CHECK_MOD (reinterpret_cast<uintptr_t> (src), alignof (DstT));
return reinterpret_cast<DstT*> (src);
#pragma GCC diagnostic pop
}
};
#endif