June 2017
Intermediate to advanced
532 pages
12h 59m
English
Before we even touched operator<< for output streams, we implemented the print_args function. Due to its variadic argument nature, it accepts any number and type of arguments, as long as the first one is an ostream instance:
template <typename T, typename ... Ts>void print_args(ostream &os, const T &v, const Ts &...vs){ os << v; (void)initializer_list<int>{((os << ", " << vs), 0)...};}
This function prints the first item, v, and then prints all the other items from the parameter pack, vs. We print the first item individually because we want to have all items interleaved with ", " but we do not want this string leading or trailing the whole list (as in "1, 2, 3, " or ", 1, 2, 3"). We learned about the initializer_list ...
Read now
Unlock full access