This was a great question! While researching this via this link, I came up with the following, which admittedly isn't much different than the provided solution there. Learn something everyday...check!
#include <iostream>
#include <string>
#include <boost/static_assert.hpp>
using namespace std;
template<typename D, typename B>
class IsDerivedFrom
{
class No { };
class Yes { No no[3]; };
static Yes Test(B*); // declared, but not defined
static No Test(...); // declared, but not defined
public:
enum { IsDerived = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };
};
class Base
{
public:
virtual ~Base() {};
};
class A : public Base
{
int i;
};
class B : public Base
{
bool b;
};
class C
{
string z;
};
template <class T1, class T2>
class BasePair
{
public:
BasePair(T1 first, T2 second)
:m_first(first),
m_second(second)
{
typedef IsDerivedFrom<T1, Base> testFirst;
typedef IsDerivedFrom<T2, Base> testSecond;
// Compile time check do...
BOOST_STATIC_ASSERT(testFirst::IsDerived == true);
BOOST_STATIC_ASSERT(testSecond::IsDerived == true);
// For runtime check do..
if (!testFirst::IsDerived)
cout << "\tFirst is NOT Derived!\n";
if (!testSecond::IsDerived)
cout << "\tSecond is NOT derived!\n";
}
private:
T1 m_first;
T2 m_second;
};
int main(int argc, char *argv[])
{
A a;
B b;
C c;
cout << "Creating GOOD pair\n";
BasePair<A, B> good(a, b);
cout << "Creating BAD pair\n";
BasePair<C, B> bad(c, b);
return 1;
}