point: prefer constexpr functions over static variables

This commit is contained in:
Danny Robson 2016-12-12 17:06:04 +11:00
parent 4bf160934c
commit f6116eb63e
4 changed files with 19 additions and 12 deletions

View File

@ -62,10 +62,6 @@ namespace util::debug {
} }
//-----------------------------------------------------------------------------
template <size_t S, typename T>
const util::point<S,T> util::point<S,T>::ORIGIN (T {0});
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#define INSTANTIATE_S_T(S,T) \ #define INSTANTIATE_S_T(S,T) \
template struct util::point<S,T>; \ template struct util::point<S,T>; \

View File

@ -34,7 +34,7 @@ namespace util {
template <size_t D> point<D,T> homog (void) const; template <size_t D> point<D,T> homog (void) const;
static const point<S,T> ORIGIN; static constexpr point<S,T> origin (void);
void sanity (void) const; void sanity (void) const;
}; };

View File

@ -18,7 +18,8 @@
#include <algorithm> #include <algorithm>
///------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
/// expand point to use homogenous coordinates of a higher dimension. /// expand point to use homogenous coordinates of a higher dimension.
/// ie, fill with (0,..,0,1) /// ie, fill with (0,..,0,1)
template <size_t S, typename T> template <size_t S, typename T>
@ -45,7 +46,17 @@ util::point<S,T>::homog (void) const
} }
//------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
constexpr
util::point<S,T>
util::point<S,T>::origin (void)
{
return point<S,T> {0};
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T, typename U> template <size_t S, typename T, typename U>
typename std::common_type<T,U>::type typename std::common_type<T,U>::type
util::distance (point<S,T> a, point<S,U> b) util::distance (point<S,T> a, point<S,U> b)
@ -58,7 +69,7 @@ util::distance (point<S,T> a, point<S,U> b)
} }
//------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T, typename U> template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type constexpr typename std::common_type<T,U>::type
util::distance2 (point<S,T> a, point<S,U> b) util::distance2 (point<S,T> a, point<S,U> b)
@ -72,7 +83,7 @@ util::distance2 (point<S,T> a, point<S,U> b)
} }
//------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T, typename U> template <typename T, typename U>
typename std::common_type<T,U>::type typename std::common_type<T,U>::type
util::octile (point2<T> a, point2<U> b) util::octile (point2<T> a, point2<U> b)
@ -96,7 +107,7 @@ util::octile (point2<T> a, point2<U> b)
} }
//------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T, typename U> template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type constexpr typename std::common_type<T,U>::type
util::manhattan (point<S,T> a, point<S,U> b) util::manhattan (point<S,T> a, point<S,U> b)
@ -110,7 +121,7 @@ util::manhattan (point<S,T> a, point<S,U> b)
} }
//------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T, typename U> template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type constexpr typename std::common_type<T,U>::type
util::chebyshev(point<S,T> a, point<S,U> b) util::chebyshev(point<S,T> a, point<S,U> b)

View File

@ -25,7 +25,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
util::region<S,T>::region (extent_t _extent): util::region<S,T>::region (extent_t _extent):
region (point_t::ORIGIN, _extent) region (point_t::origin (), _extent)
{ {
debug::sanity (*this); debug::sanity (*this);
} }