To illustrate the situation, let's assume a minimal example: a Vector
template class taking its dimension as a non-type template parameter. This class will provide x()
, y()
(etc.) accessors when the dimension allows it:
template <int N_dimension>
class Vector
{
public:
// ctors, etc.
int &x();
template <class = std::enable_if_t<(N_dimension>2)>> int &y();
private:
std::array<int, N_dimension> mData;
};
Yet, this does not work, because enable_if
can only be applied on deduced template parameters.
Our current workaround looks cumbersome:
template <int N=N_dimension, class = std::enable_if_t<(N>2)>> int &y();
Moreover, it also requires a static-assert in the definition to make sure it is fool-proof (because now client code could give an explicit value to N
that does not match the real dimension).
Is there a more direct approach to express this in C++?
Aucun commentaire:
Enregistrer un commentaire