If I do
struct A{};
struct C:private A{};
typedef char (&yes)[1];
typedef char (&no)[2];
template <typename B, typename D>
struct Host
{
operator B*() const;
operator D*();
};
template <typename B, typename D>
struct is_base_of
{
template <typename T>
static yes check(D*, T);
static no check(B*, int);
static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
};
int main(){
std::cout<<is_base_of<A,C>::value<<std::endl;
}
I get a 1. I would like to get a 0 when C is a private A, and a 1 when C is a public A.
[the code is derived from How does `is_base_of` work? ]
Do you have access to a compiler with C++11 support?
If so, you can combine Chad’s use of
static_castwithdecltypeto create a very simple type trait implementation (as demonstrated in this question). As per Jonathan Wakely’s suggestion, the references have been replaced with pointers to avoid false positives whenDdefines anoperator B&().When using gcc 4.7: