Browse Source

init vector with variadic template loop, final for buffer

Kajetan Johannes Hammerle 2 years ago
parent
commit
d1a274ea71
3 changed files with 15 additions and 9 deletions
  1. 14 7
      math/Vector.h
  2. 1 1
      utils/Buffer.h
  3. 0 1
      utils/Utils.h

+ 14 - 7
math/Vector.h

@@ -18,15 +18,22 @@ public:
 
     template<typename... Args>
     Vector(float a, Args&&... args) {
-        const int size = sizeof...(args) + 1;
-        float init[size] = {a, args...};
-        static_assert(N == size,
-                      "vector size and amount of float arguments do not match");
-        for(int i = 0; i < N; i++) {
-            values[i] = init[i];
-        }
+        init<0>(a, args...);
+    }
+
+private:
+    template<int I>
+    void init() {
+        static_assert(I == N, "vector parameters do not match its size");
     }
 
+    template<int I, typename T, typename... Args>
+    void init(T a, Args&&... args) {
+        values[I] = a;
+        init<I + 1>(args...);
+    }
+
+public:
     Vector& setAngles(float, float) = delete;
     Vector cross(const Vector&) const = delete;
 

+ 1 - 1
utils/Buffer.h

@@ -1,7 +1,7 @@
 #ifndef BUFFER_H
 #define BUFFER_H
 
-class Buffer {
+class Buffer final {
     int length;
     int capacity;
     char* buffer;

+ 0 - 1
utils/Utils.h

@@ -2,7 +2,6 @@
 #define UTILS_H
 
 namespace Utils {
-
     template<typename T>
     T interpolate(const T& a, const T& b, float f) {
         return a * (1.0f - f) + b * f;