Suppose one has a class hierarchy, without multiple inheritance:
struct TOP{};
struct L : TOP{};
struct R : TOP{};
struct LL : L{};
struct LR : L{};
struct RL : R{};
struct RR : R{};
Is it possible to write a metafunction that will return the type of the common base of two types? (it could return void
if not common base class exists.)
For example
common_base<RR, R>::type == R
common_base<RL, RR>::type == R
common_base<LL, RR>::type == TOP
common_base<LL, std::string>::type == void
Obviously this wouldn't work with multiple inhertance, but I am focused in the the single inheritance case.
First, it doesn't seem to be possible without some introspection of the base class. So, I have this easier problem, do it in such a way that each clase knows its base (by an internal base
type), for example:
struct LR : L{using base = L;};
Even in this way, I cannot seem to get the metaprogramming right.
Also I read somewhere (I can't find it now) that GCC has some extensions to detect common base class. Is that the case?
There was at some point bases and direct_bases
in std::tr2 but that wasn't included. Some versions of gcc have it. Using these perhaps you can get what you want.