关于最不常见的祖先算法有很多问题,但是这个不同,因为我试图在编译时确定 LCA,而且我的树既不是二叉树也不是搜索树,即使我的简化版本可能看起来像一。
假设您有一堆包含成员 typedef 的结构parent
,这是另一个类似的结构:
struct G
{
typedef G parent; // 'root' node has itself as parent
};
struct F
{
typedef G parent;
};
struct E
{
typedef G parent;
};
struct D
{
typedef F parent;
};
struct C
{
typedef F parent;
};
struct B
{
typedef E parent;
};
struct A
{
typedef E parent;
};
它们共同构成一棵树,例如
A B C D
\ / \ /
E F
\ /
\ /
\ /
G
注意:结构之间没有继承关系。
我想做的是创建一个类型特征least_common_ancestor
,这样:
least_common_ancestor<A, B>::type; // E
least_common_ancestor<C, D>::type; // F
least_common_ancestor<A, E>::type; // E
least_common_ancestor<A, F>::type; // G
最好的方法是什么?
我不关心算法的复杂性,特别是因为树的深度很小,而是我正在寻找最简单的元程序,它会得到正确的答案。
编辑:我需要能够使用 msvc2013 以及其他编译器构建解决方案,因此constexpr
首选没有答案。