mercredi 4 mai 2016

Logic of std::is_base_of in C++ 11

While going through some of the C++ concepts I stumbled upon std::is_base_of logic.

Googling around for logic produced the below code, but I am not able to understand it.

Could somebody explain me how it works?

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;
}

Aucun commentaire:

Enregistrer un commentaire