Sorry for being vague with my question, but I just don't understand what does this function does and how. Code from here:
template<typename ... T> auto sum (T ... t) { typename std::common_type<T...>::type result{}; //zero-initialization? (void)std::initializer_list<int>{(result += t, 0)...}; //why 0 is here return result; } I don't know why but this piece of code seems so odd, it don't look like C++ to me. Semantically, this function sums all the parameters in a result variable, obviously. But I completely do not understand why it was written that way. Using initializer_list here seem like a trick to trigger iteration over arguments in parameter pack but still...
Why initializer_list is explicitly was cast to void? To not take up extra memory?
And how iteration over parameter pack is going? Why not for example (void)std::initializer_list<int>{(result += t)...}; (it does not compile by the way).
return (... + t);Before that, pack expansion could only appear in certain contexts, e.g. in a brace-init list. So to perform calculations over a pack, you arranged a dummy brace-init list where each initializer had a side effect you wanted. The value of the initializer doesn't matter, only its side effect.(result += t, 0)uses a comma operator - it says "evaluateresult += t, then produce integer0as the value".voidwas to avoid warning for some compilers for unused expression.std::initializer_list<int>{(result += t)...};doesn't compile presumably because(result += t)is not of typeintor convertible toint. In contrast, the type of(result += t, 0)is always the type of0, which isint.