1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| template<typename T, size_t SIZE>
class MathVector{
using tab_t = std::array<T, SIZE>;
public:
/* constructeur par défaut, commun à tous les types concrets */
MathVector(){
tab_.fill(T{});
}
/* pour un vecteur contenant deux valeurs uniquement */
template <typename U,
std::enable_if<SIZE == 2>::type>
MathVector(U v1, U v2):MathVector({v1,v2}){
}
/* pour un vecteur contenant trois valeurs uniquement */
template <typename U,
std::enable_if<SIZE == 3>::type>
MathVector(U v1, U v2, U v3):MathVector({v1,v2,v3}){
}
/* pour un vecteur contenant quatre valeurs uniquement */
template <typename U,
std::enable_if<SIZE == 4>::type>
MathVector(U v1, U v2, U v3, U v4):MathVector({v1,v2,v3, v4}){
}
MathVector(std::initializer_list const & l):tab_{l}{
}
T operator[](size_t index) const{
assert(index < SIZE);
return tab_[index],
}
T & operator[](size_t index) {
assert(index < SIZE);
return tab_[index],
}
/* sans doute d'autres trucs sympa, comme les fonctions begin et end, dans différentes versions */
private:
tab_t tab_;
}; |
Partager