From f6116eb63e30538ae59081103fa3886f3a824688 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 12 Dec 2016 17:06:04 +1100 Subject: [PATCH] point: prefer constexpr functions over static variables --- point.cpp | 4 ---- point.hpp | 2 +- point.ipp | 23 +++++++++++++++++------ region.cpp | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/point.cpp b/point.cpp index 5a9e62b1..fc1bef83 100644 --- a/point.cpp +++ b/point.cpp @@ -62,10 +62,6 @@ namespace util::debug { } -//----------------------------------------------------------------------------- -template -const util::point util::point::ORIGIN (T {0}); - //----------------------------------------------------------------------------- #define INSTANTIATE_S_T(S,T) \ template struct util::point; \ diff --git a/point.hpp b/point.hpp index 8b40a5b3..c9bf480c 100644 --- a/point.hpp +++ b/point.hpp @@ -34,7 +34,7 @@ namespace util { template point homog (void) const; - static const point ORIGIN; + static constexpr point origin (void); void sanity (void) const; }; diff --git a/point.ipp b/point.ipp index 38b146f3..2635686b 100644 --- a/point.ipp +++ b/point.ipp @@ -18,7 +18,8 @@ #include -///------------------------------------------------------------------------ + +/////////////////////////////////////////////////////////////////////////////// /// expand point to use homogenous coordinates of a higher dimension. /// ie, fill with (0,..,0,1) template @@ -45,7 +46,17 @@ util::point::homog (void) const } -//------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// +template +constexpr +util::point +util::point::origin (void) +{ + return point {0}; +} + + +/////////////////////////////////////////////////////////////////////////////// template typename std::common_type::type util::distance (point a, point b) @@ -58,7 +69,7 @@ util::distance (point a, point b) } -//------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// template constexpr typename std::common_type::type util::distance2 (point a, point b) @@ -72,7 +83,7 @@ util::distance2 (point a, point b) } -//------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// template typename std::common_type::type util::octile (point2 a, point2 b) @@ -96,7 +107,7 @@ util::octile (point2 a, point2 b) } -//------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// template constexpr typename std::common_type::type util::manhattan (point a, point b) @@ -110,7 +121,7 @@ util::manhattan (point a, point b) } -//------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// template constexpr typename std::common_type::type util::chebyshev(point a, point b) diff --git a/region.cpp b/region.cpp index bcc8f950..0203010d 100644 --- a/region.cpp +++ b/region.cpp @@ -25,7 +25,7 @@ //----------------------------------------------------------------------------- template util::region::region (extent_t _extent): - region (point_t::ORIGIN, _extent) + region (point_t::origin (), _extent) { debug::sanity (*this); }