Nikita Petrenko
Nikita Petrenko

Reputation: 1088

Get deepest class in CRTP inheritance chain

I would like to know how to solve the following problem (C++17): suppose there are several template classes, inherited from each other in CRTP-like fashion (single inheritance only). For a given instantiated template base class, find the class that is furthest from it down the inheritance chain.

I first thought that is should be pretty easy, but was not able to accomplish this.

To simplify, suppose that every root and every intermediate class has using DerivedT = Derived in its public area.

Example:

template <class T>
struct GetDeepest {
    using Type = ...;
};

template <class T>
struct A {
    using DerivedT = T;
};

template <class T>
struct B : public A<B<T>> {
    using DerivedT = T;
};

struct C : B<C> {
};

struct D : A<D> {
};

GetDeepest<A<D>>::Type == D;
GetDeepest<B<C>>::Type == C;
GetDeepest<A<B<C>>>::Type == C;
...

First implementation I've tried:

template <class T>
struct GetDeepest {
    template <class Test, class = typename Test::DerivedT>
    static std::true_type Helper(const Test&);
    static std::false_type Helper(...);

    using HelperType = decltype(Helper(std::declval<T>()));
    using Type = std::conditional_t<std::is_same_v<std::true_type, HelperType>,
                    GetDeepest<typename T::DerivedT>::Type,
                    T>;
};

Second implementation I've tried:

template <class T>
struct HasNext {
    template <class Test, class = typename Test::DerivedT>
    static std::true_type Helper(const Test&);
    static std::false_type Helper(...);

    using HelperType = decltype(Helper(std::declval<T>()));
    static const bool value = std::is_same_v<std::true_type, HelperType>;
};

template <class T>
auto GetDeepestHelper(const T& val) {
    if constexpr(HasNext<T>::value) {
        return GetDeepestHelper(std::declval<typename T::DerivedT>());
    } else {
        return val;
    }
}

template <class T>
struct GetDeepest {
    using Type = decltype(GetDeepestLevelHelper(std::declval<T>()));
};

None of them compile.

First one -- because of incomplete type of GetDeepest<T> in statement using Type = ..., second because of recursive call of a function with auto as a return type.

Is it even possible to implement GetDeepest<T> class with such properties? Now I'm very curious, even if it might be not the best way to accomplish what I want.

Thanks!

Upvotes: 3

Views: 139

Answers (1)

Quimby
Quimby

Reputation: 19113

I'm not sure if I fully understand the question so feel free to ask me in comments.

But I think this should work:

#include <type_traits>

template<typename T>
struct GetDeepest
{
    using Type = T;
};

template<template<typename> class DT, typename T>
struct GetDeepest<DT<T>>
{
    using Type = typename GetDeepest<T>::Type;
};

template <class T>
struct A {
    using DerivedT = T;
};

template <class T>
struct B : public A<B<T>> {
    using DerivedT = T;
};

struct C : B<C> {
};

struct D : A<D> {
};

int main()
{
    static_assert(std::is_same<GetDeepest<A<D>>::Type, D>::value);
    static_assert(std::is_same<GetDeepest<B<C>>::Type, C>::value);
    static_assert(std::is_same<GetDeepest<A<B<C>>>::Type, C>::value);

}

Upvotes: 4

Related Questions