我有一个问题,我需要发现两种类型(具有一个或零个基类)的共同祖先(如果存在)。是否有可能建立一个类型特征来解决这个问题?在代码中:
template<typename T1, typename T2>
struct closest_common_ancestor
{
typedef XXX type; // what goes here?
};
给定以下类型:
struct root {};
struct child1 : root {};
struct child2 : root {};
struct child3 : child2 {};
struct unrelated {};
closest_common_ancestor
将导致以下类型:
closest_common_ancestor<root, child1>::type == root
closest_common_ancestor<child1, child2>::type == root
closest_common_ancestor<child3, child1>::type == root
closest_common_ancestor<child3, child2>::type == child2
closest_common_ancestor<unrelated, child1>::type == error
我相信如果我可以检查一个类型是否有零个或一个基类,如果有,那么我可以解决这个问题,如果是,那么该类型的名称。这可能吗?