Is that such a horror?
template <typename ...P> struct sum_type; template <typename T> struct sum_type<T> { using type = T; }; template <typename T, typename ...P> struct sum_type<T, P...> { using type = decltype(T()+typename sum_type<P...>::type()); }; template <typename ...P> using sum_type_t = typename sum_type<P...>::type; template <typename T> inline T sum(T t) { return t; } template <typename T, typename ...P> inline sum_type_t<T, P... > sum(T t, P... p) { return t + sum(p...); }
Peeped in Discovering Modern C ++.
You can simply transfer anything to the function using three points :) - of type sum(double a, ...)
, but I don’t recognize such a possibility within the function to recognize different types without templates.
Update Here is another solution:
template <typename T> inline T sum(T t) { return t; } template <typename T, typename ...P> inline auto sum(T t, P... p) { return [](auto a, auto b) { return a + b; }(t,sum(p...)); } int main() { std::cout << sum(2, 2.5, ' ', false) << std::endl; }