A while ago, a solution to print out std::tuple was posted here. For the most part I get what's happening. I'm having trouble understanding whats happening in the print_tuple function though.
template<class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple(std::basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){
using swallow = int[];
(void)swallow{0, (void(os << (Is == 0? "" : ", ") << std::get<Is>(t)), 0)...};
}
I don't get what's happening in the body of this function. As far as I can tell, it has something to do with unpacking Is
. I get that the condition, Is == 0
is checking to see if we're at the head element.
So what's going on?
int[]
array from an initializer list, where each element is 0 but prints one element of the tuple as a side effect (via comma operator). The use of initializer list is just to get into a context where pack expansion would work. – Caesarswallow{...}
construct is an initialization list for int[]. I did not catch that at first glance. – Stinkwood