From 44a08a551f5ea90a45ed7adcd48d13eb5a7164d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 15 May 2012 15:35:12 +0200 Subject: [PATCH] Added static_assert to "initializer list" Matrix and Vector constructor. C++ allows creating arrays with initializer lists shorter than array length, but for vectors and matrices it will be error prone and hard to debug. Removed deleted constructor, as it is now catched with static_assert as well. Also this was possible before (and wasn't catched with the deleted constructor), now isn't: Matrix<2, int> a(1, 2); --- src/Math/Matrix.h | 9 ++++----- src/Math/Matrix3.h | 4 ---- src/Math/Matrix4.h | 4 ---- src/Math/Vector.h | 4 +++- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Math/Matrix.h b/src/Math/Matrix.h index a6a26ef9f..29903db66 100644 --- a/src/Math/Matrix.h +++ b/src/Math/Matrix.h @@ -72,6 +72,7 @@ template class Matrix { * @param next Next column vectors */ template inline constexpr static Matrix from(const Vector& first, const U&... next) { + static_assert(sizeof...(next)+1 == size, "Improper number of arguments passed to Matrix from Vector constructor"); return from(typename Implementation::GenerateSequence::Type(), first, next...); } @@ -100,10 +101,6 @@ template class Matrix { _data[size*i+i] = value; } - #ifndef DOXYGEN_GENERATING_OUTPUT - template explicit Matrix(U) = delete; - #endif - /** * @brief Initializer-list constructor * @param first First value @@ -112,7 +109,9 @@ template class Matrix { * Note that the values are in column-major order. */ #ifndef DOXYGEN_GENERATING_OUTPUT - template inline constexpr Matrix(T first, U... next): _data{first, next...} {} + template inline constexpr Matrix(T first, U... next): _data{first, next...} { + static_assert(sizeof...(next)+1 == size*size, "Improper number of arguments passed to Matrix constructor"); + } #else template inline constexpr Matrix(T first, U... next); #endif diff --git a/src/Math/Matrix3.h b/src/Math/Matrix3.h index e82da033d..eede7cce6 100644 --- a/src/Math/Matrix3.h +++ b/src/Math/Matrix3.h @@ -37,10 +37,6 @@ template class Matrix3: public Matrix<3, T> { 0.0f, 0.0f, value ) {} - #ifndef DOXYGEN_GENERATING_OUTPUT - template explicit Matrix3(U) = delete; - #endif - /** @copydoc Matrix::Matrix(T, U...) */ #ifndef DOXYGEN_GENERATING_OUTPUT template inline constexpr Matrix3(T first, U... next): Matrix<3, T>(first, next...) {} diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index ee29378a2..8fb94c644 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -105,10 +105,6 @@ template class Matrix4: public Matrix<4, T> { 0.0f, 0.0f, 0.0f, value ) {} - #ifndef DOXYGEN_GENERATING_OUTPUT - template explicit Matrix4(U) = delete; - #endif - /** @copydoc Matrix::Matrix(T, U...) */ #ifndef DOXYGEN_GENERATING_OUTPUT template inline constexpr Matrix4(T first, U... next): Matrix<4, T>(first, next...) {} diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 2f34a0fa2..9007ce6f4 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -91,7 +91,9 @@ template class Vector { * @param next Next values */ #ifndef DOXYGEN_GENERATING_OUTPUT - template inline constexpr Vector(T first, U... next): _data{first, next...} {} + template inline constexpr Vector(T first, U... next): _data{first, next...} { + static_assert(sizeof...(next)+1 == size, "Improper number of arguments passed to Vector constructor"); + } #else template inline constexpr Vector(T first, U... next); #endif