0

Could you please explain below code ? It compiles fine. Its related to check whether given class is base of another class.

template<typename D, typename B>
class IsDerivedFromHelper
{
    class No { };
    class Yes { No no[3]; };

    static Yes Test( B* );
    static No Test( ... );
public:
    enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };

};


template <class C, class P>
bool IsDerivedFrom() {
    return IsDerivedFromHelper<C, P>::Is;
}
jpb123
  • 315
  • 1
  • 2
  • 7
  • How come static method without body can get compiled ? – jpb123 Jun 01 '18 at 14:17
  • Note that c++ offers [`std::is_base_of`](http://en.cppreference.com/w/cpp/types/is_base_of) since c++11. – François Andrieux Jun 01 '18 at 14:18
  • The `Test` static member functions aren't called, you only care about their return type as overload resolution is being used to check that `D*` can be implicitly converted to `B*`. – François Andrieux Jun 01 '18 at 14:19
  • Thanks @FrançoisAndrieux, I understood. But can you please help me understand below line. enum { Is = sizeof(Test(static_cast(0))) == sizeof(Yes) }; I meant this enum is a type ? or value ? confused ? – jpb123 Jun 01 '18 at 14:26

0 Answers0