Commit 02b8d498 authored by Quentin Khan's avatar Quentin Khan
Browse files

Refactor variadic sums to remove recursion

parent 639ed29f
......@@ -265,16 +265,12 @@ private:
}
/** Variadic sum recursion end */
template<typename A>
A sum(const A& a) {
return a;
}
/** Variadic sum */
template<typename A, typename B, typename... Args>
A sum(const A& a, const B& b, const Args&... args) {
return sum(a+b, args...);
template<typename... Args>
auto sum(const Args&... args) -> typename std::common_type<Args...>::type {
typename std::common_type<Args...>::type res = {};
auto l = {(res += args, 0) ...}; (void)l;
return res;
}
......
......@@ -564,36 +564,19 @@ private:
}
/** \brief Variadic sum recursion end
*
* \tparam T Return type
*
* \param a Sum last sub-sum
*
* \return The parameter a
*/
template<typename T>
T sum(const T& a) {
return a;
}
/** \brief Variadic sum
*
* This sum is implemented in a recursive manner
*
* \tparam T First operand type
* \tparam U Second operand type
* \tparam Ts Other operands types
* \tparam Ts Operands types
*
* \param a First operand
* \param b Second operand
* \param bs Other operands
* \param ts Operands
*
* \return The sum a + b + bs...
* \return The sum of ts...
*/
template<typename T, typename U, typename... Ts>
T sum(const T& a, const U& b, const Ts&... bs) {
return sum(a+b, bs...);
template<typename... Ts>
auto sum(const Ts&... ts) -> typename std::common_type<Ts...>::type {
typename std::common_type<Ts...>::type res = {};
auto l = {(res += ts, 0)...}; (void)l;
return res;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment