implement static cast on C++

155 views Asked by At

I am trying to implement static cast. I need to check if the types T and U are implicitly convertible, if not check if one inherites from another. I can write a class to check each on of them, but I cant understand how to check implicit convert and if it doesnt compile check the inheritance.

all the checking need to on at compile time

1

There are 1 answers

0
101010 On

You could use type_traits and in particular std::is_convertible in combination with std::is_base_of:

template<typename T, typename U, typename std::enable_if<std::is_convertible<T, U>::value ||
std::is_base_of<T, U>::value>::type* = nullptr>
T mystatic_cast(U &u)
{
    return u;
}

Live Demo