In C++17, fold expression is available, so to print arguments, we could use
#define EOL '\n'
template<typename ...Args>
void output_argus(Args&&... args)
{
(cout << ... << args) << EOL;
}
int main()
{
output_argus(1, "test", 5.6f);
}
having the output1test5.6
What if I would like using the fold expression appending an extra character '\n'
to each element to get the following results?
1
test
5.6
Is that even possible? If yes, how?
What if I would like using the fold expression appending an extra character '\n' to each element to get the following results?
You can use the power of the comma operator
((std::cout << args << std::endl), ...);
or, as suggested by Quentin (thanks) and as you asked, you can simply use \n
instead of std::endl
(to avoid multiple flushing of the stream)
((std::cout << args << '\n'), ...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With