Browse Source

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);
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
44a08a551f
  1. 9
      src/Math/Matrix.h
  2. 4
      src/Math/Matrix3.h
  3. 4
      src/Math/Matrix4.h
  4. 4
      src/Math/Vector.h

9
src/Math/Matrix.h

@ -72,6 +72,7 @@ template<size_t size, class T> class Matrix {
* @param next Next column vectors * @param next Next column vectors
*/ */
template<class ...U> inline constexpr static Matrix<size, T> from(const Vector<size, T>& first, const U&... next) { template<class ...U> inline constexpr static Matrix<size, T> from(const Vector<size, T>& 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<size>::Type(), first, next...); return from(typename Implementation::GenerateSequence<size>::Type(), first, next...);
} }
@ -100,10 +101,6 @@ template<size_t size, class T> class Matrix {
_data[size*i+i] = value; _data[size*i+i] = value;
} }
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class U> explicit Matrix(U) = delete;
#endif
/** /**
* @brief Initializer-list constructor * @brief Initializer-list constructor
* @param first First value * @param first First value
@ -112,7 +109,9 @@ template<size_t size, class T> class Matrix {
* Note that the values are in column-major order. * Note that the values are in column-major order.
*/ */
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
template<class ...U> inline constexpr Matrix(T first, U... next): _data{first, next...} {} template<class ...U> 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 #else
template<class ...U> inline constexpr Matrix(T first, U... next); template<class ...U> inline constexpr Matrix(T first, U... next);
#endif #endif

4
src/Math/Matrix3.h

@ -37,10 +37,6 @@ template<class T> class Matrix3: public Matrix<3, T> {
0.0f, 0.0f, value 0.0f, 0.0f, value
) {} ) {}
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class U> explicit Matrix3(U) = delete;
#endif
/** @copydoc Matrix::Matrix(T, U...) */ /** @copydoc Matrix::Matrix(T, U...) */
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
template<class ...U> inline constexpr Matrix3(T first, U... next): Matrix<3, T>(first, next...) {} template<class ...U> inline constexpr Matrix3(T first, U... next): Matrix<3, T>(first, next...) {}

4
src/Math/Matrix4.h

@ -105,10 +105,6 @@ template<class T> class Matrix4: public Matrix<4, T> {
0.0f, 0.0f, 0.0f, value 0.0f, 0.0f, 0.0f, value
) {} ) {}
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class U> explicit Matrix4(U) = delete;
#endif
/** @copydoc Matrix::Matrix(T, U...) */ /** @copydoc Matrix::Matrix(T, U...) */
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
template<class ...U> inline constexpr Matrix4(T first, U... next): Matrix<4, T>(first, next...) {} template<class ...U> inline constexpr Matrix4(T first, U... next): Matrix<4, T>(first, next...) {}

4
src/Math/Vector.h

@ -91,7 +91,9 @@ template<size_t size, class T> class Vector {
* @param next Next values * @param next Next values
*/ */
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
template<class ...U> inline constexpr Vector(T first, U... next): _data{first, next...} {} template<class ...U> 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 #else
template<class ...U> inline constexpr Vector(T first, U... next); template<class ...U> inline constexpr Vector(T first, U... next);
#endif #endif

Loading…
Cancel
Save