From 4bf160934c2a093c5010b97c296c0cd3322698e9 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 12 Dec 2016 17:04:22 +1100 Subject: [PATCH] vector: prefer constexpr functions over static variables --- test/coord.cpp | 2 +- test/vector.cpp | 4 ++-- vector.cpp | 12 ------------ vector.hpp | 4 ++-- vector.ipp | 20 ++++++++++++++++++++ 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/test/coord.cpp b/test/coord.cpp index 1b7c7b70..255017c1 100644 --- a/test/coord.cpp +++ b/test/coord.cpp @@ -34,7 +34,7 @@ main (void) auto vec = util::vector4f (0.5f); t.expect_eq (vec, util::normalised (vec), "normalisation of normalised vector"); - t.expect_eq (sum (util::vector4f::ONES), 4.f, "elementwise summation"); + t.expect_eq (sum (util::vector4f::ones ()), 4.f, "elementwise summation"); return t.status (); } diff --git a/test/vector.cpp b/test/vector.cpp index e1134f5e..394e48d9 100644 --- a/test/vector.cpp +++ b/test/vector.cpp @@ -111,8 +111,8 @@ main () test_polar (tap); test_euler (tap); - tap.expect (!is_normalised (util::vector3f::ZERO), "zero isn't normalised"); - tap.expect (!is_normalised (util::vector3f::ONES), "ones isn't normalised"); + tap.expect (!is_normalised (util::vector3f::zeros ()), "zeros isn't normalised"); + tap.expect (!is_normalised (util::vector3f::ones ()), "ones isn't normalised"); tap.expect_eq ( util::hypot (util::vector3f{0,1,2} - util::vector3f{3,2,4}), diff --git a/vector.cpp b/vector.cpp index 8ee61a3d..da76eced 100644 --- a/vector.cpp +++ b/vector.cpp @@ -112,18 +112,6 @@ util::cartesian_to_spherical (vector<3,T> c) } -/////////////////////////////////////////////////////////////////////////////// -template -const util::vector -util::vector::ONES (T{1}); - - -//----------------------------------------------------------------------------- -template -const util::vector -util::vector::ZERO (T{0}); - - /////////////////////////////////////////////////////////////////////////////// template void diff --git a/vector.hpp b/vector.hpp index 4f34b6d9..72c511ed 100644 --- a/vector.hpp +++ b/vector.hpp @@ -30,8 +30,8 @@ namespace util { template vector homog (void) const; // constants - static const vector ONES; - static const vector ZERO; + static constexpr vector ones (void); + static constexpr vector zeros (void); void sanity (void) const; }; diff --git a/vector.ipp b/vector.ipp index 6e4750fe..83e8fab3 100644 --- a/vector.ipp +++ b/vector.ipp @@ -46,3 +46,23 @@ util::cross (util::vector<3,T> a, util::vector<3,T> b) a.x * b.y - a.y * b.x }; } + + +/////////////////////////////////////////////////////////////////////////////// +template +constexpr +util::vector +util::vector::ones (void) +{ + return vector {1}; +} + + +//----------------------------------------------------------------------------- +template +constexpr +util::vector +util::vector::zeros (void) +{ + return vector {0}; +}