#include <iostream>
#include <typeinfo>
#include <type_traits>
using namespace std;
struct Grandma {};
struct Mom : Grandma {};
struct Son : Mom {};
struct Grandchild : Son {};
template<typename...Ts>
struct LCA;
template<typename T1, typename T2, typename...Ts>
struct LCA<T1, T2, Ts...>
{
using base = typename std::conditional
<
std::is_base_of<T1, T2>::value, T1,
typename std::conditional <
std::is_base_of<T2, T1>::value, T2, void
>::type
>::type;
using type = typename LCA<base, Ts...>::type;
};
template<typename T>
struct LCA<T>
{
using type = T;
};
int main()
{
LCA<Son, Grandchild, Son>::type obj1;
LCA<Son, Grandchild, Mom, Son>::type obj2;
LCA<Mom, Mom, Mom>::type obj3;
cout << "obj1 type = " << typeid(obj1).name() << endl;
cout << "obj2 type = " << typeid(obj2).name() << endl;
cout << "obj3 type = " << typeid(obj3).name() << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHlwZWluZm8+CiNpbmNsdWRlIDx0eXBlX3RyYWl0cz4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgR3JhbmRtYSAgICAgICAgICB7fTsKc3RydWN0IE1vbSA6IEdyYW5kbWEgICAge307CnN0cnVjdCBTb24gOiBNb20gICAgICAgIHt9OwpzdHJ1Y3QgR3JhbmRjaGlsZCA6IFNvbiB7fTsKCgp0ZW1wbGF0ZTx0eXBlbmFtZS4uLlRzPgpzdHJ1Y3QgTENBOwoKdGVtcGxhdGU8dHlwZW5hbWUgVDEsIHR5cGVuYW1lIFQyLCB0eXBlbmFtZS4uLlRzPgpzdHJ1Y3QgTENBPFQxLCBUMiwgVHMuLi4+CnsKCXVzaW5nIGJhc2UgPSB0eXBlbmFtZSBzdGQ6OmNvbmRpdGlvbmFsCgk8CgkJc3RkOjppc19iYXNlX29mPFQxLCBUMj46OnZhbHVlLCBUMSwKCQl0eXBlbmFtZSBzdGQ6OmNvbmRpdGlvbmFsIDwKCQkJc3RkOjppc19iYXNlX29mPFQyLCBUMT46OnZhb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