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 38 39 40 41
| template<class T, unsigned int d> class Vector;
template<class T, unsigned int d> class VectorGeneric{
protected:
T coord[d];
public:
VectorGeneric(const T (&coords)[d]){
memcpy(coord, coords, sizeof(T)*d);
}
template<typename... Ts>
VectorGeneric(Ts&&... coords) : coord{std::forward<Ts>(coords)...} {
static_assert(sizeof...(Ts) == d, "You must provide d arguments.");
}
// Un exemple de fonction membre
Vector<T,d> operator+(const VectorGeneric<T,d>& vector) const{
T coords[d];
for(uint i=0; i<d; ++i){
coords[i]=vector.coord[i]+coord[i];
}
return Vector<T,d>(coords);
}
};
template<class T, unsigned int d> class Vector: public VectorGeneric<T, d>{
Vector(const T (&coords)[d]):VectorGeneric<T, d>(coords){}
template<typename... Ts>
Vector(Ts&&... coords) : VectorGeneric<T, d>(std::forward<Ts>(coords)...) {}
};
template<class T> class Vector<T, 2>: public VectorGeneric<T, 2>{
public:
T& x = this->coord[0];
T& y = this->coord[1];
Vector(const T (&coords)[2]): VectorGeneric<T, 2>(coords){}
Vector(T x, T y) : VectorGeneric<T, 2>(x,y){}
};
template<class T> class Vector<T, 3>: public VectorGeneric<T, 3>{
public:
T& x = this->coord[0];
T& y = this->coord[1];
T& z = this->coord[2];
Vector(const T (&coords)[3]): VectorGeneric<T, 3>(coords){}
Vector(T x, T y, T z) : VectorGeneric<T, 3>(x, y, z){}
}; |
Partager