jeudi 15 octobre 2020

variadic template struct, recursive type declaration, is it possible?

Is there any way to declare a type recursively? I was almost there but I got a compile error.

Use case example:

typename OP_T<char, float, int>::M var //var is actually a float

Code

template <typename ...T>
struct OP_T{};

template <typename T0, typename T1>
struct OP_T<T0, T1> {
    using M = typename x_traits<T0, T1>::MULT_T;
};

template <typename T0, typename ...Ts>
struct OP_T<T0, Ts...> {
    using M = typename OP_T<T0, Ts...>::M; // error: 'M' is not a member of 'OP_T' 
};

This is x_traits simplified

template<typename T>
struct x_traits_default {
    typedef T MULT_T;
};

template<typename T1, typename T2>
struct x_traits {};

template<typename T2>
struct x_traits<double, T2> : public x_traits_default<double> {};

template<typename T1>
struct x_traits<T1, double> : public x_traits_default<double> {};

Aucun commentaire:

Enregistrer un commentaire