coord/ops: use common_type for results

This commit is contained in:
Danny Robson 2015-10-19 12:02:07 +11:00
parent d73e86b374
commit 5ed95ec952

View File

@ -95,35 +95,37 @@ namespace util {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// scalar operators // scalar operators
#define SCALAR_OP(OP) \ #define SCALAR_OP(OP) \
template < \ template < \
size_t S, \ size_t S, \
typename T, \ typename T, \
typename U, \ typename U, \
template <size_t,typename> class K \ template <size_t,typename> class K \
> \ > \
typename std::enable_if<std::is_fundamental<U>::value, K<S,T>>::type \ K<S,typename std::common_type<T,U>::type> \
operator OP (U u, K<S,T> k) \ operator OP (U u, K<S,T> k) \
{ \ { \
K<S,T> out; \ using out_t = typename std::common_type<T,U>::type; \
for (size_t i = 0; i < S; ++i) \ K<S,out_t> out; \
out[i] = u OP k[i]; \ for (size_t i = 0; i < S; ++i) \
return out; \ out[i] = u OP k[i]; \
} \ return out; \
\ } \
template < \ \
size_t S, \ template < \
typename T, \ size_t S, \
typename U, \ typename T, \
template <size_t,typename> class K \ typename U, \
> \ template <size_t,typename> class K \
typename std::enable_if<std::is_fundamental<U>::value, K<S,T>>::type \ > \
operator OP (K<S,T> k, U u) \ K<S,typename std::common_type<T,U>::type> \
{ \ operator OP (K<S,T> k, U u) \
K<S,T> out; \ { \
for (size_t i = 0; i < S; ++i) \ using out_t = typename std::common_type<T,U>::type; \
out[i] = k[i] OP u; \ K<S,out_t> out; \
return out; \ for (size_t i = 0; i < S; ++i) \
out[i] = k[i] OP u; \
return out; \
} }
SCALAR_OP(+) SCALAR_OP(+)
@ -136,18 +138,28 @@ namespace util {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
#define SCALAR_OP(OP) \ // scalar assignment operators.
template < \ //
size_t S, \ // we must check the operands/results do not need casting to store in the
typename T, \ // destination type to avoid silent errors accumulating.
template <size_t,typename> class K \ #define SCALAR_OP(OP) \
> \ template < \
K<S,T>& \ size_t S, \
operator OP (K<S,T> &k, T t) \ typename T, \
{ \ typename U, \
for (size_t i = 0; i < S; ++i) \ template <size_t,typename> class K \
k[i] OP t; \ > \
return k; \ typename std::enable_if< \
std::is_same< \
T, \
typename std::common_type<T,U>::type>::value, \
K<S,T> \
>::type& \
operator OP (K<S,T> &k, U u) \
{ \
for (size_t i = 0; i < S; ++i) \
k[i] OP u; \
return k; \
} }
SCALAR_OP(+=) SCALAR_OP(+=)