Monday, April 17, 2006

IsDerivedFrom

From GotW

// Example 3(a): An IsDerivedFrom helper
//
template
class IsDerivedFrom
{
private:
class Yes { char a[1]; };
class No { char a[10]; };

static Yes Test( B* ); // undefined
static No Test( ... ); // undefined

public:
enum { Is = sizeof(Test(static_cast(0))) == sizeof(Yes) ? 1 : 0 };
};

No comments: