From 81e7a0996f932418611feeacff71c4f82b02f27e Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 25 Feb 2021 00:05:51 +0700 Subject: [PATCH] add ctors without initialization --- README.md | 66 ++++++---------- headers/vmath.hpp/vmath_fwd.hpp | 6 ++ headers/vmath.hpp/vmath_mat.hpp | 136 ++++++++++++++++---------------- headers/vmath.hpp/vmath_qua.hpp | 13 ++- headers/vmath.hpp/vmath_vec.hpp | 31 ++++---- untests/vmath_mat_tests.cpp | 16 ++++ untests/vmath_qua_tests.cpp | 5 ++ untests/vmath_vec_tests.cpp | 16 ++++ 8 files changed, 158 insertions(+), 131 deletions(-) diff --git a/README.md b/README.md index df551af..3619723 100644 --- a/README.md +++ b/README.md @@ -85,10 +85,11 @@ class vec_base; template < typename T > class vec_base { public: - T x = T{0}; - T y = T{0}; + T x, y; + + vec_base(); + explicit vec_base(uninit_t); - vec_base() = default; explicit vec_base(T v); vec_base(T x, T y); }; @@ -96,11 +97,11 @@ public: template < typename T > class vec_base { public: - T x = T{0}; - T y = T{0}; - T z = T{0}; + T x, y, z; + + vec_base(); + explicit vec_base(uninit_t); - vec_base() = default; explicit vec_base(T v); vec_base(T x, T y, T z); vec_base(const vec_base& xy, T z); @@ -112,12 +113,11 @@ public: template < typename T > class vec_base { public: - T x = T{0}; - T y = T{0}; - T z = T{0}; - T w = T{0}; + T x, y, z, w; + + vec_base(); + explicit vec_base(uninit_t); - vec_base() = default; explicit vec_base(T v); vec_base(T x, T y, T z, T w); vec_base(const vec_base& xy, T z, T w); @@ -151,10 +151,6 @@ public: static inline size_t size = Size; - vec() = default; - vec(const vec&) = default; - vec& operator=(const vec&) = default; - void swap(vec& other); iterator begin(); @@ -221,12 +217,11 @@ template < typename T > class mat_base { public: using row_type = vec; + row_type rows[2]; - row_type rows[2] = { - row_type{T{1}, T{0}}, - row_type{T{0}, T{1}}}; + mat_base(); + explicit mat_base(uninit_t); - mat_base() = default; explicit mat_base(T d); explicit mat_base(const row_type& d); @@ -243,13 +238,11 @@ template < typename T > class mat_base { public: using row_type = vec; + row_type rows[3]; - row_type rows[3] = { - row_type{T{1}, T{0}, T{0}}, - row_type{T{0}, T{1}, T{0}}, - row_type{T{0}, T{0}, T{1}}}; + mat_base(); + explicit mat_base(uninit_t); - mat_base() = default; explicit mat_base(T d); explicit mat_base(const row_type& d); @@ -276,14 +269,11 @@ template < typename T > class mat_base { public: using row_type = vec; + row_type rows[4]; - row_type rows[4] = { - row_type{T{1}, T{0}, T{0}, T{0}}, - row_type{T{0}, T{1}, T{0}, T{0}}, - row_type{T{0}, T{0}, T{1}, T{0}}, - row_type{T{0}, T{0}, T{0}, T{1}}}; + mat_base(); + explicit mat_base(uninit_t); - mat_base() = default; explicit mat_base(T d); explicit mat_base(const row_type& d); @@ -332,10 +322,6 @@ public: static inline size_t size = Size; - mat() = default; - mat(const mat&) = default; - mat& operator=(const mat&) = default; - void swap(mat& other); iterator begin(); @@ -398,10 +384,12 @@ using ptrdiff4x4_t = mat; template < typename T > class qua_base { public: - vec v = vec{T{0}}; - T s = T{1}; + vec v; + T s; qua_base(); + explicit qua_base(uninit_t); + qua_base(T vx, T vy, T vz, T s); qua_base(const vec& v, T s); @@ -432,10 +420,6 @@ public: static inline size_t size = 4; - qua() = default; - qua(const qua&) = default; - qua& operator=(const qua&) = default; - void swap(qua& other); iterator begin(); diff --git a/headers/vmath.hpp/vmath_fwd.hpp b/headers/vmath.hpp/vmath_fwd.hpp index 95d1e22..e0ff237 100644 --- a/headers/vmath.hpp/vmath_fwd.hpp +++ b/headers/vmath.hpp/vmath_fwd.hpp @@ -38,6 +38,12 @@ #define VMATH_HPP_THROW_IF(pred, ...)\ ( (pred) ? VMATH_HPP_THROW(__VA_ARGS__) : (void)0 ) +namespace vmath_hpp +{ + struct uninit_t { explicit uninit_t() = default; }; + inline constexpr uninit_t uninit{}; +} + namespace vmath_hpp { template < typename T, std::size_t Size > diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index 42bbba8..4993d24 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -20,29 +20,31 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; - - row_type rows[2] = { - row_type{T{1}, T{0}}, - row_type{T{0}, T{1}}}; + row_type rows[2]; public: - constexpr mat_base() = default; + constexpr mat_base() + : rows{ + {T{1}, T{0}}, + {T{0}, T{1}}} {} + + constexpr explicit mat_base(uninit_t) {} constexpr explicit mat_base(T d) : rows{ - row_type{d, T{0}}, - row_type{T{0}, d}} {} + {d, T{0}}, + {T{0}, d}} {} constexpr explicit mat_base(const row_type& d) : rows{ - row_type{d[0], T{0}}, - row_type{T{0}, d[1]}} {} + {d[0], T{0}}, + {T{0}, d[1]}} {} constexpr mat_base( T m11, T m12, T m21, T m22) : rows{ - row_type{m11, m12}, - row_type{m21, m22}} {} + {m11, m12}, + {m21, m22}} {} constexpr mat_base( const row_type& row0, @@ -54,34 +56,36 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; - - row_type rows[3] = { - row_type{T{1}, T{0}, T{0}}, - row_type{T{0}, T{1}, T{0}}, - row_type{T{0}, T{0}, T{1}}}; + row_type rows[3]; public: - constexpr mat_base() = default; + constexpr mat_base() + : rows{ + {T{1}, T{0}, T{0}}, + {T{0}, T{1}, T{0}}, + {T{0}, T{0}, T{1}}} {} + + constexpr explicit mat_base(uninit_t) {} constexpr explicit mat_base(T d) : rows{ - row_type{d, T{0}, T{0}}, - row_type{T{0}, d, T{0}}, - row_type{T{0}, T{0}, d}} {} + {d, T{0}, T{0}}, + {T{0}, d, T{0}}, + {T{0}, T{0}, d}} {} constexpr explicit mat_base(const row_type& d) : rows{ - row_type{d[0], T{0}, T{0}}, - row_type{T{0}, d[1], T{0}}, - row_type{T{0}, T{0}, d[2]}} {} + {d[0], T{0}, T{0}}, + {T{0}, d[1], T{0}}, + {T{0}, T{0}, d[2]}} {} constexpr mat_base( T m11, T m12, T m13, T m21, T m22, T m23, T m31, T m32, T m33) : rows{ - row_type{m11, m12, m13}, - row_type{m21, m22, m23}, - row_type{m31, m32, m33}} {} + {m11, m12, m13}, + {m21, m22, m23}, + {m31, m32, m33}} {} constexpr mat_base( const row_type& row0, @@ -93,16 +97,16 @@ namespace vmath_hpp::detail const mat_base& m, const vec_base& v) : rows{ - row_type{m.rows[0], T{0}}, - row_type{m.rows[1], T{0}}, - row_type{v, T{1}}} {} + {m.rows[0], T{0}}, + {m.rows[1], T{0}}, + {v, T{1}}} {} constexpr explicit mat_base( const mat_base& other) : rows{ - row_type{other.rows[0], T{0}}, - row_type{other.rows[1], T{0}}, - row_type{T{0}, T{0}, T{1}}} {} + {other.rows[0], T{0}}, + {other.rows[1], T{0}}, + {T{0}, T{0}, T{1}}} {} constexpr explicit operator mat() const { return { @@ -115,28 +119,30 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; - - row_type rows[4] = { - row_type{T{1}, T{0}, T{0}, T{0}}, - row_type{T{0}, T{1}, T{0}, T{0}}, - row_type{T{0}, T{0}, T{1}, T{0}}, - row_type{T{0}, T{0}, T{0}, T{1}}}; + row_type rows[4]; public: - constexpr mat_base() = default; + constexpr mat_base() + : rows{ + {T{1}, T{0}, T{0}, T{0}}, + {T{0}, T{1}, T{0}, T{0}}, + {T{0}, T{0}, T{1}, T{0}}, + {T{0}, T{0}, T{0}, T{1}}} {} + + constexpr explicit mat_base(uninit_t) {} constexpr explicit mat_base(T d) : rows{ - row_type{d, T{0}, T{0}, T{0}}, - row_type{T{0}, d, T{0}, T{0}}, - row_type{T{0}, T{0}, d, T{0}}, - row_type{T{0}, T{0}, T{0}, d}} {} + {d, T{0}, T{0}, T{0}}, + {T{0}, d, T{0}, T{0}}, + {T{0}, T{0}, d, T{0}}, + {T{0}, T{0}, T{0}, d}} {} constexpr explicit mat_base(const row_type& d) : rows{ - row_type{d[0], T{0}, T{0}, T{0}}, - row_type{T{0}, d[1], T{0}, T{0}}, - row_type{T{0}, T{0}, d[2], T{0}}, - row_type{T{0}, T{0}, T{0}, d[3]}} {} + {d[0], T{0}, T{0}, T{0}}, + {T{0}, d[1], T{0}, T{0}}, + {T{0}, T{0}, d[2], T{0}}, + {T{0}, T{0}, T{0}, d[3]}} {} constexpr mat_base( T m11, T m12, T m13, T m14, @@ -144,10 +150,10 @@ namespace vmath_hpp::detail T m31, T m32, T m33, T m34, T m41, T m42, T m43, T m44) : rows{ - row_type{m11, m12, m13, m14}, - row_type{m21, m22, m23, m24}, - row_type{m31, m32, m33, m34}, - row_type{m41, m42, m43, m44}} {} + {m11, m12, m13, m14}, + {m21, m22, m23, m24}, + {m31, m32, m33, m34}, + {m41, m42, m43, m44}} {} constexpr mat_base( const row_type& row0, @@ -160,26 +166,26 @@ namespace vmath_hpp::detail const mat_base& m, const vec_base& v) : rows{ - row_type{m.rows[0], T{0}}, - row_type{m.rows[1], T{0}}, - row_type{m.rows[2], T{0}}, - row_type{v, T{1}}} {} + {m.rows[0], T{0}}, + {m.rows[1], T{0}}, + {m.rows[2], T{0}}, + {v, T{1}}} {} constexpr explicit mat_base( const mat_base& other) : rows{ - row_type{other.rows[0], T{0}, T{0}}, - row_type{other.rows[1], T{0}, T{0}}, - row_type{T{0}, T{0}, T{1}, T{0}}, - row_type{T{0}, T{0}, T{0}, T{1}}} {} + {other.rows[0], T{0}, T{0}}, + {other.rows[1], T{0}, T{0}}, + {T{0}, T{0}, T{1}, T{0}}, + {T{0}, T{0}, T{0}, T{1}}} {} constexpr explicit mat_base( const mat_base& other) : rows{ - row_type{other.rows[0], T{0}}, - row_type{other.rows[1], T{0}}, - row_type{other.rows[2], T{0}}, - row_type{T{0}, T{0}, T{0}, T{1}}} {} + {other.rows[0], T{0}}, + {other.rows[1], T{0}}, + {other.rows[2], T{0}}, + {T{0}, T{0}, T{0}, T{1}}} {} constexpr explicit operator mat() const { return { @@ -223,10 +229,6 @@ namespace vmath_hpp using base_type::mat_base; using base_type::rows; - constexpr mat() = default; - constexpr mat(const mat&) = default; - constexpr mat& operator=(const mat&) = default; - void swap(mat& other) noexcept(std::is_nothrow_swappable_v) { for ( std::size_t i = 0; i < Size; ++i ) { using std::swap; diff --git a/headers/vmath.hpp/vmath_qua.hpp b/headers/vmath.hpp/vmath_qua.hpp index fedac84..531ff09 100644 --- a/headers/vmath.hpp/vmath_qua.hpp +++ b/headers/vmath.hpp/vmath_qua.hpp @@ -16,10 +16,13 @@ namespace vmath_hpp::detail template < typename T > class qua_base { public: - vec v = vec{T{0}}; - T s = T{1}; + vec v; + T s; public: - constexpr qua_base() = default; + constexpr qua_base() + : v{T{0}}, s{1} {} + + constexpr explicit qua_base(uninit_t) {} constexpr qua_base(T vx, T vy, T vz, T s) : v{vx, vy, vz}, s{s} {} @@ -84,10 +87,6 @@ namespace vmath_hpp using base_type::qua_base; using base_type::operator[]; - constexpr qua() = default; - constexpr qua(const qua&) = default; - constexpr qua& operator=(const qua&) = default; - void swap(qua& other) noexcept(std::is_nothrow_swappable_v) { for ( std::size_t i = 0; i < size; ++i ) { using std::swap; diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index 2aaa027..b2f3042 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -16,10 +16,12 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - T x = T{0}; - T y = T{0}; + T x, y; public: - constexpr vec_base() = default; + constexpr vec_base() + : x{0}, y{0} {} + + constexpr explicit vec_base(uninit_t) {} constexpr explicit vec_base(T v) : x{v}, y{v} {} @@ -47,11 +49,12 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - T x = T{0}; - T y = T{0}; - T z = T{0}; + T x, y, z; public: - constexpr vec_base() = default; + constexpr vec_base() + : x{0}, y{0}, z{0} {} + + constexpr explicit vec_base(uninit_t) {} constexpr explicit vec_base(T v) : x{v}, y{v}, z{v} {} @@ -91,12 +94,12 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - T x = T{0}; - T y = T{0}; - T z = T{0}; - T w = T{0}; + T x, y, z, w; public: - constexpr vec_base() = default; + constexpr vec_base() + : x{0}, y{0}, z{0}, w{0} {} + + constexpr explicit vec_base(uninit_t) {} constexpr explicit vec_base(T v) : x{v}, y{v}, z{v}, w{v} {} @@ -177,10 +180,6 @@ namespace vmath_hpp using base_type::vec_base; using base_type::operator[]; - constexpr vec() = default; - constexpr vec(const vec&) = default; - constexpr vec& operator=(const vec&) = default; - void swap(vec& other) noexcept(std::is_nothrow_swappable_v) { for ( std::size_t i = 0; i < Size; ++i ) { using std::swap; diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index 1daf0ab..99b15c3 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -40,6 +40,22 @@ TEST_CASE("vmath/mat") { } SUBCASE("ctors") { + { + mat m2; + CHECK(m2.rows[0] == int2(1,0)); + CHECK(m2.rows[1] == int2(0,1)); + + mat m3; + CHECK(m3.rows[0] == int3(1,0,0)); + CHECK(m3.rows[1] == int3(0,1,0)); + CHECK(m3.rows[2] == int3(0,0,1)); + + mat m4; + CHECK(m4.rows[0] == int4(1,0,0,0)); + CHECK(m4.rows[1] == int4(0,1,0,0)); + CHECK(m4.rows[2] == int4(0,0,1,0)); + CHECK(m4.rows[3] == int4(0,0,0,1)); + } { STATIC_CHECK(int2x2()[0] == int2(1,0)); STATIC_CHECK(int2x2()[1] == int2(0,1)); diff --git a/untests/vmath_qua_tests.cpp b/untests/vmath_qua_tests.cpp index bd0d160..99b20af 100644 --- a/untests/vmath_qua_tests.cpp +++ b/untests/vmath_qua_tests.cpp @@ -30,6 +30,11 @@ TEST_CASE("vmath/qua") { } SUBCASE("ctors") { + { + qua q; + CHECK(q.v == int3(0,0,0)); + CHECK(q.s == 1); + } { STATIC_CHECK(qfloat{}.v == uapprox3(0.f)); STATIC_CHECK(qfloat{}.s == uapprox(1.f)); diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 72a11ab..59785d8 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -40,6 +40,22 @@ TEST_CASE("vmath/vec") { } SUBCASE("ctors") { + { + vec i2; + CHECK(i2.x == 0); + CHECK(i2.y == 0); + + vec i3; + CHECK(i3.x == 0); + CHECK(i3.y == 0); + CHECK(i3.z == 0); + + vec i4; + CHECK(i4.x == 0); + CHECK(i4.y == 0); + CHECK(i4.z == 0); + CHECK(i4.w == 0); + } { STATIC_CHECK(int2().x == 0); STATIC_CHECK(int2().y == 0);