I'm trying to account for the fact that my program will contain calculations such as:
v(t) = w ∗ v(t−1)+ c1∗r1∗( pBest − x(t −1)) + c2∗r2∗(gBest − x(t−1))
where v
, x
, pBest
, gBest
are vectors (std::array
s). I'm a little worried if I make correct use of the move semantics.
Here's the relevant snippet:
template<class T, class F, size_t l> std::array<T, l> operator *(std::array<T, l>&& lhs, const F rhs ) {
static_assert(std::is_arithmetic<T>::value, "The type parameter of the first argument is not of arithmetic type");
static_assert(std::is_arithmetic<F>::value, "The passed scalar is not of arithmetic type");
std::array<T, l> tmp = std::move(lhs);
for(size_t i = 0; i < l; ++i)
tmp[i] *= rhs;
return tmp;
}
//calling the above operator
template<class T, class F, size_t l> std::array<T, l> operator *(const F rhs, std::array<T, l>&& lhs ) {
return std::move(lhs) * rhs;
}
POST-ANSWER EDIT: As Loki Astari pointed out this SHOULD NOT be the sole implementation for operator*
as this can bind to lvalues and will destroy them.