The source code below is excerpted from MongoDB.
I understand that detect_clone_factory_type_member_impl serves the purpose of checking whether the type T possesses a clone_factory_type or not. However, what puzzles me is that since Derived inherits from both T and Fallback, regardless of whether T has a clone_factory_type, Derived should always possess a clone_factory_type. Wouldn't that mean the first test function, No& test(typename U::clone_factory_type*), should always be chosen?
When I actually run experiments, this routine works as expected, which makes me very curious as to how this mechanism functions. I would appreciate the expertise of various professionals.
template <typename T>
struct detect_clone_factory_type_member_impl {
struct Fallback {
struct clone_factory_type {};
};
struct Derived : T, Fallback {};
using Yes = char[2];
using No = char[1];
template <typename U>
static No& test(typename U::clone_factory_type*);
template <typename U>
static Yes& test(U*);
static constexpr bool value = sizeof(test<Derived>(nullptr)) == sizeof(Yes);
using type = typename std::integral_constant<bool, value>::type;
};