I asked myself: "What C ++ constructs make a distinction between direct inheritance and indirect?" Let me remind you that C ++ constructors of derived types directly call constructors only for their direct bases. So, the code is as follows:
Derived::Derived() : Base() {}
Valid only if Base is a direct Derived base. And since you enter the rtti.h code into the body of Derived , you can endure the restriction that this method is only directly visible inside the derived class itself (i.e., it is not general, like the hypothetical type_traits::is_direct_base_of , but not necessary) .
So, since we probably don't want to interfere with the default constructors as such, but how do I add some special goals?
rtti.h :
#define IS_DIRECT_BASE_OF(_B_, _A_) _B_(rtti_tag tag) : _A_(tag) \ { assert(false); }
This code compiles for me with g ++ 4.2; if I insert a new class into the inheritance hierarchy, then the statement breaks and the compilation fails with what, in my opinion, is a fairly descriptive diagnosis:
In constructor 'B::B(rtti_tag)': error: type 'A' is not a direct base of 'B' ...
source share