A coworker recently showed me some code that he found online. It appears to allow compile time determination of whether a type has an \"is a\" relationship with another type. I
It declares two overloaded functions named test
, one taking a Base
and one taking anything (...)
, and returning different types.
It then calls the function with a Derived
and checks the size of its return type to see which overload is called. (It actually calls the function with the return value of a function that returns Derived
, to avoid using memory)
Because enum
s are compile-time constants, all of this is done within the type system at compile-time. Since the functions don't end up getting called at runtime, it doesn't matter that they have no bodies.