I was going through a nice question for how is_base_of is implemented in boost (which decides if a given class
is a base of another class
at compile time].
Seeing such code for the 1st time, I got amazed that how things can be made to work so nicely! However, many steps I got confused to understand (after reading all the answers). So, I was wondering that if this functionality can be implemented alternatively. I tried following:
template<class B, class D>
struct is_base_of
{
template<typename T> struct dummy {};
struct Child : D, dummy<int> {};
static B* Check (B*);
template<class T> static char Check (dummy<T>*);
static const bool value = (sizeof(Check((Child*)0)) == sizeof(B*));
};
It works fine as per expectation for the general case.
The only problem is for private/protected
inheritance. It goes and choose the expected function, but also shows error as:- is an inaccessible base of ...
. I would appreciate if someone can suggest any little modification missing to the code to solve this problem (If not the functionality wise then at least to get rid of the error message).
[Note: I have assume that char
and B*
will always be of different sizes and avoided typecasting of typical 'yes' and 'no']