With C++17 is it possible to detect if a struct/class has any base?

Yes, this is possible, at least for aggregates.

First we construct a class template that is convertible to any proper base of its template parameter:

template<class T>
struct any_base {
    operator T() = delete;
    template<class U, class = std::enable_if_t<std::is_base_of_v<U, T>>> operator U();
};

Then we detect whether a template parameter T is aggregate constructible from a value of type any_base<T>:

template<class, class = void> struct has_any_base : std::false_type {};
template<class T>
struct has_any_base<T, std::void_t<decltype(T{any_base<T>{}})>> : std::true_type {};

Example.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)