I am trying to understand below code. Copied directly from Jason Turner youtube video
#include <iostream>
#include <sstream>
#include <vector>
template<typename ...T>
std::vector<std::string> print(const T& ...t)
{
std::vector<std::string> retval;
std::stringstream ss;
(void)std::initializer_list<int>{
(
ss.str(""),
ss << t,
retval.push_back(ss.str()),
0)...
};
return retval;
}
int main()
{
for( const auto &s : print("Hello", "World", 5.4, 1.1, 2.2) ) {
std::cout << s << "\n";
}
}
Questions :
- Can someone give the expanded view of the code within initializer_list? I am having hard time visualising how the statement expands per argument? Does the ss.str(""), ss << t and then the push_back happen for each parameter in the pack OR they are just executed once? I am not able to visualize how the expanded initializer list will look like?
- Why do we need the dummy '0' at the end of the initializer_list? What happens if i don't have that?
- How can i easily view the ... expansion in code i shared?