]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Require C++11 for building
[libs/math.git] / source / linal / vector.h
index c9b2a179fe5fd0ce595e4dda796b3c2d7b13ab5d..610ac2103a21ab83e0b58f88905e6d07c6962d2d 100644 (file)
@@ -84,9 +84,9 @@ public:
        use by Matrix row accessor. */
        Vector(const T *, unsigned);
 
-       Vector(T, T);
-       Vector(T, T, T);
-       Vector(T, T, T, T);
+       template<typename... Args>
+       Vector(T, Args...);
+
        template<typename U>
        Vector(const Vector<U, N> &);
 
@@ -126,30 +126,15 @@ inline Vector<T, N>::Vector(const T *d, unsigned stride)
                (*this)[i] = d[i*stride];
 }
 
-/* The compiler won't instantiate these unless they are used.  Trying to use
-them on the wrong class results in an error. */
-template<typename T, unsigned N>
-inline Vector<T, N>::Vector(T x_, T y_)
-{
-       this->VectorComponents<T, 2>::x = x_;
-       this->VectorComponents<T, 2>::y = y_;
-}
-
-template<typename T, unsigned N>
-inline Vector<T, N>::Vector(T x_, T y_, T z_)
-{
-       this->VectorComponents<T, 3>::x = x_;
-       this->VectorComponents<T, 3>::y = y_;
-       this->VectorComponents<T, 3>::z = z_;
-}
-
 template<typename T, unsigned N>
-inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
-{
-       this->VectorComponents<T, 4>::x = x_;
-       this->VectorComponents<T, 4>::y = y_;
-       this->VectorComponents<T, 4>::z = z_;
-       this->VectorComponents<T, 4>::w = w_;
+template<typename... Args>
+inline Vector<T, N>::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<T> { static_cast<T>(v)... })
+               (*this)[i++] = c;
 }
 
 template<typename T, unsigned N>
@@ -174,9 +159,9 @@ template<typename T, unsigned N>
 inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
 {
        Vector<T, N+1> r;
+       r[0] = s;
        for(unsigned i=0; i<N; ++i)
-               r[i] = v[i];
-       r[N] = s;
+               r[i+1] = v[i];
        return r;
 }