jeudi 7 janvier 2021

Can I get name / reference to specialization value inside template specialization?

My code:

enum class list{one, two};
template <list T> class Base;
template <> class Base <list::one>{
    A a{list::one};
    B b{list::one};
    C c{list::one};
};
template <> class Base <list::two>{
    B b{list::two};
    C c{list::two};
    D d{list::two};
};

But I would like to avoid duplicating code, and use reference to specialization value, like this:

template <> class Base <list::one>{
    A a{T};
    B b{T};
    C c{T};
};
template <> class Base <list::two>{
    B b{T};
    C c{T};
    D d{T};
};

I can make sludge temporary variable, but does not look good too:

template <> class Base <list::one>{
    list T = list::one;
    A a{T};
    B b{T};
    C c{T};
};

Is there are any way to get reference to template specialization value?

Aucun commentaire:

Enregistrer un commentaire