Can someone improve on my horrible implementation of are_siblings below? It is merely guessing at all possible parents and then deciding if the two classes are siblings based on those parent candidates.
#include <type_traits>
template <bool...> struct Or;
template <bool First, bool... Rest>
struct Or<First, Rest...> :
std::integral_constant<bool, First ? true : Or<Rest...>::value> {};
template <>
struct Or<> : std::false_type {};
template <typename T, typename U, typename... Parents>
struct are_siblings :
Or<(std::is_base_of<Parents, T>::value && std::is_base_of<Parents, U>::value)...> {};
// Test
#include <iostream>
struct A {};
struct B : A {};
struct C {};
struct D : A, C {};
struct E : B, C {};
int main() {
std::cout << std::boolalpha << are_siblings<D,E, A,B,C>::value << '\n'; // true
std::cout << std::boolalpha << are_siblings<C,D, A,B,E>::value << '\n'; // false
std::cout << std::boolalpha << are_siblings<B,E, A,B,E>::value << '\n'; // true !?
// (but B is a parent of E!)
}
It also incorrectly evaluates that C and D are siblings if C is derived from A.
Aucun commentaire:
Enregistrer un commentaire