cast: account for pointers-to-const in ffs

This commit is contained in:
Danny Robson 2019-01-02 13:48:59 +11:00
parent 82b0bb2245
commit 75d5f84ff6

View File

@ -188,7 +188,17 @@ namespace cruft::cast {
#if defined(COMPILER_GCC) #if defined(COMPILER_GCC)
#pragma GCC diagnostic ignored "-Wcast-function-type" #pragma GCC diagnostic ignored "-Wcast-function-type"
#endif #endif
return reinterpret_cast<DstT> (src); // If we're casting a pointer then we may need to first use a
// const_cast if we want to avoid any warnings about casting, eg in
// the case of `const ValueT*` to `ValueT*`
if constexpr (std::is_pointer_v<SrcT>) {
using value_type = std::remove_pointer_t<SrcT>;
return reinterpret_cast<DstT> (
const_cast<std::remove_cv_t<value_type>*> (src)
);
} else {
return reinterpret_cast<DstT> (src);
}
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
} }