samedi 3 décembre 2016

Put array-like object as template value argument

I want to implement a

template<class ValueType,UnitTag x>
clsas Quantity;

With operators like

template<UnitTag y>
Quantity<T,x+y> operator*(Quantity<T,y> b)
    {return Quantity<T,x+y>(m_value*b.value());}

(it is also possible to add quantities, but only of the same kind)

Operations on UnitTags has to be done element-wise

x_0 + y_0 = z_0
x_1 + y_1 = z_1
x_2 + y_2 = z_2
x_3 + y_3 = z_3
...

The most straight forward way would be to let UnitTag be a constexpr array. But that cannot be used as a template argument. As a workaround, I can do it with a large integer type, and use a small number of bits to represent each dimension. However, this gives a limitation on the number of dimensions, or the power of each dimension: For example, with 64 bits and 16 dimensions, I only covers [L^(-8), L^7]. This should be good for most applications, but the class cannot deal with radicals, since this requires fractional powers.

Is there any way to pass more than 64 bits of information in a template argument? For example, is it possible to do element-wise operations on a template parameter pack?

Aucun commentaire:

Enregistrer un commentaire