The comments mention TR2 reflection proposal which is "abandoned" but yet has an implementation in gcc. The code below is based on SO post: Finding base class at compile time
Preparing a tuple to hold the types returned from TR2
#include<tr2/type_traits>
template<typename T>
struct types_as_tuple;
template<typename... Ts>
struct types_as_tuple<std::tr2::__reflection_typelist<Ts...>>
{
typedef std::tuple<Ts...> type;
};
Checking for duplicate type in a tuple
template<typename T>
struct duplicate_type;
template<typename T1, typename T2, typename... Ts>
struct duplicate_type<std::tuple<T1, T2, Ts...>> {
constexpr static bool value = std::is_same<T1, T2>::value
|| duplicate_type<std::tuple<T1, Ts...>>::value;
|| duplicate_type<std::tuple<T2, Ts...>>::value
};
template<typename T1, typename T2>
struct duplicate_type<std::tuple<T1, T2>> {
constexpr static bool value = std::is_same<T1, T2>::value;
};
TypesPrinter
template<typename T>
struct TypesPrinter;
template<typename T>
struct TypesPrinter<std::tuple<T>> {
constexpr TypesPrinter() {
std::cout << typeid(T).name() << ' ';
}
};
template<typename T, typename... Ts>
struct TypesPrinter<std::tuple<T, Ts...>> {
constexpr TypesPrinter() {
TypesPrinter<std::tuple<T>>();
TypesPrinter<std::tuple<Ts...>>();
}
};
Main
struct A {};
struct B: A {};
struct C {};
struct D : B, C, A {};
int main() {
//---------------------------------------------------------------
// getting all direct and indirect bases of a class with TR2
//---------------------------------------------------------------
using bases_of_D = types_as_tuple<std::tr2::bases<D>::type>::type;
//---------------------------------------------------------------
// checking for duplication at compile time
// (can also use static_assert)
//---------------------------------------------------------------
if constexpr(duplicate_type<bases_of_D>::value) {
std::cout << "duplicate base in class D" << std::endl;
}
//---------------------------------------------------------------
// printing the types
//---------------------------------------------------------------
TypesPrinter<bases_of_D>();
}
http://coliru.stacked-crooked.com/a/d556c47c660832ff