Browse Source

Possiblity for constructing Vector from one value, like in GLSL.

Marked the constructor as explicit, because we don't want mistakes like
this to happen:

    Matrix4::rotation(1.0f, deg(3.0f)); // oops, swapped axis and angle!

Instead, when calling such constructor, the type must be said
explicitly (initializer-list is forbidden, too):

    Matrix4::rotation(deg(3.0f), Vector3(1.0f)); // okay
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
a3b754b88e
  1. 9
      src/Math/Vector.h
  2. 4
      src/Math/Vector2.h
  3. 4
      src/Math/Vector3.h
  4. 3
      src/Math/Vector4.h

9
src/Math/Vector.h

@ -72,6 +72,15 @@ template<class T, size_t size> class Vector {
template<class ...U> inline constexpr Vector(T first, U&&... next);
#endif
/**
* @brief Constructor
* @param value Value for all fields
*/
inline explicit Vector(T value) {
for(size_t i = 0; i != size; ++i)
_data[i] = value;
}
/** @brief Copy constructor */
inline constexpr Vector(const Vector<T, size>& other) = default;

4
src/Math/Vector2.h

@ -36,8 +36,8 @@ template<class T> class Vector2: public Vector<T, 2> {
return *reinterpret_cast<const Vector2<T>*>(data);
}
/** @copydoc Vector::Vector */
inline constexpr Vector2() {}
/** @copydoc Vector::Vector(T) */
inline constexpr explicit Vector2(T value = T()): Vector<T, 2>(value, value) {}
/** @copydoc Vector::Vector(const Vector&) */
inline constexpr Vector2(const Vector<T, 2>& other): Vector<T, 2>(other) {}

4
src/Math/Vector3.h

@ -52,8 +52,8 @@ template<class T> class Vector3: public Vector<T, 3> {
a[0]*b[1]-a[1]*b[0]);
}
/** @copydoc Vector::Vector */
inline constexpr Vector3() {}
/** @copydoc Vector::Vector(T) */
inline constexpr explicit Vector3(T value = T()): Vector<T, 3>(value, value, value) {}
/** @copydoc Vector::Vector(const Vector&) */
inline constexpr Vector3(const Vector<T, 3>& other): Vector<T, 3>(other) {}

3
src/Math/Vector4.h

@ -43,6 +43,9 @@ template<class T> class Vector4: public Vector<T, 4> {
*/
inline constexpr Vector4(): Vector<T, 4>(T(0), T(0), T(0), T(1)) {}
/** @copydoc Vector::Vector(T) */
inline constexpr explicit Vector4(T value): Vector<T, 4>(value, value, value, value) {}
/** @copydoc Vector::Vector(const Vector&) */
inline constexpr Vector4(const Vector<T, 4>& other): Vector<T, 4>(other) {}

Loading…
Cancel
Save