Hi.
I've been struggling for weeks about how Parameter Pack extension
really works.
Preface: I over-used Google and cppreference, but I still don't get it.
For example:
1 2 3 4 5 6 7
|
template<typename... Type>
void PrintAll(Type... params)
{
(cout << ... << params) << endl;
}
PrintAll(4,"hello",5);
|
This prints 4hello5.
[1]
Why doesn't this work as well?
1 2 3 4 5 6 7
|
template<typename... Type>
void PrintAll(Type... params)
{
(cout << params << ...) << endl;
}
PrintAll(4,"hello",5);
|
This gives me error:
invalid operands to binary expression ('basic_ostream<char, std::char_traits<char> >' and '_Myt' (aka 'basic_ostream<char, std::char_traits<char> >'))
[2]
As you saw form the output, the ENDL is performed after the cout of all the elements.
I want to execute the endl after each element of the Pack
So I tried:
1 2 3 4 5
|
template<typename... Type>
void PrintAll(Type... params)
{
(cout << ... << params << endl) << endl;
}
|
Compiled with clang, it gives me this compilation error:
error: reference to overloaded function could not be resolved; did you mean to call it?
I expected something like
(cout << params1 << endl << params2 << endl << params 3) << endl;
NOTES:
1. If you're gonna link me something, it's 90% sure I already read it
2. Please, I want to understand why MY method doesn't work. If you have alternative code for accomplishing the same thing, it's well-accepted. But first I need to understand where my logic is wrong.
My main problem is that I don't understand how a Paramter Pack expansion works, how an expression is evaluated with the '...'