X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flinal%2Fvector.h;h=31597d4282410e2606590eaea2faa6d3c22c2cd9;hb=0a8781509bafe347b9c32f1106891eac18354965;hp=9a9d02822aebe554627178803b4c6bd4c2621ddc;hpb=2feea51b078cc53663e411bf80e35044c1ef4a07;p=libs%2Fmath.git diff --git a/source/linal/vector.h b/source/linal/vector.h index 9a9d028..31597d4 100644 --- a/source/linal/vector.h +++ b/source/linal/vector.h @@ -3,6 +3,7 @@ #include #include +#include namespace Msp { namespace LinAl { @@ -83,9 +84,14 @@ public: use by Matrix row accessor. */ Vector(const T *, unsigned); +#if __cplusplus >= 201103L + template + Vector(T, Args...); +#else Vector(T, T); Vector(T, T, T); Vector(T, T, T, T); +#endif template Vector(const Vector &); @@ -125,6 +131,18 @@ inline Vector::Vector(const T *d, unsigned stride) (*this)[i] = d[i*stride]; } +#if __cplusplus >= 201103L +template +template +inline Vector::Vector(T x_, Args... v) +{ + static_assert(1+sizeof...(v)==N, "Incorrect number of arguments in Vector constructor"); + (*this)[0] = x_; + unsigned i = 1; + for(auto c: std::initializer_list { static_cast(v)... }) + (*this)[i++] = c; +} +#else /* The compiler won't instantiate these unless they are used. Trying to use them on the wrong class results in an error. */ template @@ -150,6 +168,7 @@ inline Vector::Vector(T x_, T y_, T z_, T w_) this->VectorComponents::z = z_; this->VectorComponents::w = w_; } +#endif template template @@ -173,9 +192,9 @@ template inline Vector compose(T s, const Vector &v) { Vector r; + r[0] = s; for(unsigned i=0; i cross(const Vector &v1, const Vector &v2) return Vector(v1.y*v2.z-v1.z*v2.y, v1.z*v2.x-v1.x*v2.z, v1.x*v2.y-v1.y*v2.x); } +template +inline std::ostream &operator<<(std::ostream &s, const Vector &v) +{ + s << "Vector" << N << '('; + for(unsigned i=0; i