Kai-Uwe Bux said:
Sure you can say that: nobody is going to stop you. However, be informed,
that if you did, you would be asserting something false: template
instantiation in C++ is a concept totally independent from inheritance (be
it from abstract base classes or from concrete classes).
Maybe there is an underlying problem or confusion. What triggered your
question?
Oh well, I guess I was confused: reading Victors reply, I realized that I
profoundly misunderstood your question. Sorry about that.
To make up for it, here is what I use to test for public inheritance:
template < typename B, typename D >
class is_subclass {
typedef char (&no) [1];
typedef char (&yes) [2];
struct dummy {
operator D& ( void );
};
static
yes check ( B& );
static
no check ( ... );
public:
static bool const value = sizeof( check( dummy() ) ) == sizeof( yes );
}; // is_subclass
template < typename B, typename D >
class is_a {
typedef char (&no) [1];
typedef char (&yes) [2];
static
D& dummy ( void );
static
yes check ( B& );
static
no check ( ... );
public:
static bool const value = sizeof( check( dummy() ) ) == sizeof( yes );
}; // is_a
struct ABC {};
struct A : public ABC {};
struct X {
static A dummy;
operator A& () {
return dummy;
}
};
A X::dummy;
struct Y {
static ABC dummy;
operator ABC& ( void ) {
return dummy;
}
};
ABC Y::dummy;
#include <iostream>
int main ( void ) {
std::cout
<< is_subclass<ABC,ABC>::value << " "
<< is_subclass<ABC,A>::value << " "
<< is_subclass<ABC,X>::value << " "
<< is_subclass<ABC,Y>::value << "\n"
<< is_a<ABC,ABC>::value << " "
<< is_a<ABC,A>::value << " "
<< is_a<ABC,X>::value << " "
<< is_a<ABC,Y>::value << "\n";
}
Things to note:
a) I consider every class is subclass of itself.
b) is_a<> is more permissive than is_subclass. (The key is that the struct
dummy approach will prevent further user-defined conversions since we
already used one.)
c) These templates are not supposed to handle private inheritance.
Best
Kai-Uwe Bux