cast: disable invalid overloads of known using sfinae

This commit is contained in:
Danny Robson 2018-10-22 18:22:02 +11:00
parent 2bbe6dc900
commit b510c48797

View File

@ -127,22 +127,28 @@ namespace cruft::cast {
/// assert if the value is not a pointer to a subclass of T, else return /// assert if the value is not a pointer to a subclass of T, else return
/// the converted value. Note: this is only a debug-time check and is /// the converted value. Note: this is only a debug-time check and is
/// compiled out in optimised builds. /// compiled out in optimised builds.
template <typename T, typename V> template <
T* typename T,
known (V *v) typename V
>
std::enable_if_t<std::is_pointer_v<T>,T>
known (V *const v)
{ {
CHECK (dynamic_cast<T*> (v)); CHECK (dynamic_cast<T> (v));
return static_cast<T*> (v); return static_cast<T> (v);
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <typename T, typename V> template <
T& typename T,
typename V
>
std::enable_if_t<std::is_reference_v<T>, T>
known (V &v) known (V &v)
{ {
CHECK_NOTHROW (dynamic_cast<T> (v)); CHECK_NOTHROW (dynamic_cast<T> (v));
return static_cast<T> (v); return reinterpret_cast<T> (v);
} }