In a previous question the desire for something like common_base_t
was raised. It is true that the result might be ambiguous, as two classes may have more than a single shared base class.
I'm looking for a way to discover whether two classes have a shared common base, just yes or no, without asking who is this shared base.
Again I feel that this cannot be achieved with the current tools that we have in C++20 (and including the expected tools in C++23). However, maybe someone may have an idea. Using Boost or any other external library is acceptable, but not something that is not supported in the language, like the old std::tr2
.
I have failed, trying to implement (naively I'd say) something like this:
template<typename T, typename... Ts>
concept have_common_public_base =
std::derived_from<T, std::common_type_t<T, Ts...>> &&
(std::derived_from<Ts, std::common_type_t<T, Ts...>> && ...);
As std::common_type
doesn't see the common type of two distinct classes, even if they share a common base (unless one actually derives publicly from the other, directly or indirectly, which is a simple case not covering the general case).
Any idea? Maybe some magic games with dynamic_cast
?