mardi 8 septembre 2020

How to find maximum dereferenceable-level of the parameter T using template

I am designing a "dereferencer" class, for fun.

I wrote some structs and aliass :

template <class _T>
using deref_type = decltype(*std::declval<_T>());

template <class _T, class _SFINAE>
struct is_derefable : std::false_type {};

template <class _T>
struct is_derefable< _T, deref_type<_T> > : std::true_type
{
    using return_type = deref_type<_T>;
};

template<class _T>
using check_derefable = is_derefable<T, deref_type<T>>;

and let's say that there is a variable with type T = std::vector<int**>::iterator, which is the iterator dereferenced into a level-2 pointer, thus has a 3-level dereferenceability.

Here, I want to know the maximum level of "dereferenceability" of an arbitrary type T, at the compile-time.

std::cout << deref_level<std::vector<int**>::iterator>::max << std::endl; // this should prints 3

I thought that it would be way similar to generating a sequence at the compile-time: Template tuple - calling a function on each element , but I can't draw a concrete picture of it.

Here are what I've tried:

template<class _TF, class _T>
struct derefability {};

template<int _N, class _derefability>
struct deref_level;

template<int _N, class _T>
struct deref_level<_N, derefability<std::false_type, _T>>
{
    static const int max = _N;
};

template<int _N, class _T>
struct deref_level<_N, derefability<std::true_type, _T>> : 
    deref_level<_N + 1, derefability<typename check_derefable<deref_type<_T>>::type, deref_type<_T>>>{};

deref_level<0, derefability<check_derefable<T>::type, T>::max;

but it does not work...(compiler says that max is not a member of tje class) What went wrong?

Aucun commentaire:

Enregistrer un commentaire