In this expression
((std::cout << args <<" "), 0)...
The comma operator is used. The value of the expression is the second operand after the comma, that is 0. As a result, the character array is initialized with zeros. At the same time, there is a side effect of calculating the first operand of the comma operator as output to the stream of arguments passed to the function.
To make it clearer, simply replace 0, for example, with the 'A' character. Below is a demonstration program.
#include <iostream> template <typename ...Args> void print( Args && ...args ) { char compile_time_buffer[sizeof...(Args)] = { ( (std::cout << args <<" "), 'A' )... }; std::cout.write( compile_time_buffer, sizeof...(Args) ); } int main() { print( 1, 2, 3 ); return 0; }
Her console output is as follows.
1 2 3 AAA
You can make this program more interesting. For example,
#include <iostream> template <typename ...Args> void print( Args && ...args ) { char c = 'A'; char compile_time_buffer[sizeof...(Args) + 1] = { ( std::cout << args <<" ", c++ )... }; std::cout << compile_time_buffer << std::endl; } int main() { print( 1, 2, 3 ); return 0; }
Her console output is as follows.
1 2 3 ABC
Here is another simple example of using the comma operator when initializing a variable.
int x = ( std::cout << "袠薪懈褑懈邪谢懈蟹邪褑懈褟 x. x = ", 10 ); std::cout << x << std::endl;
The console will display
袠薪懈褑懈邪谢懈蟹邪褑懈褟 x. x = 10