I don't understand the error message when trying to pass a variable number of initializer lists:
template<typename... Values>
void foo(Values...)
{
}
int main()
{
foo(1, 2, 3, "hello", 'a'); // OK
foo({1}, {2, 3}); // ERROR
}
The error message complains about too many arguments:
prog.cpp: In function ‘int main()’:
prog.cpp:9:20: error: too many arguments to function
‘void foo(Values ...) [with Values = {}]’
foo({1}, {2, 3});
^
prog.cpp:2:6: note: declared here
void foo(Values...)
^
However, should I not be able to pass as many arguments as I want? [ideone link]
The problem is indeed deducibility, as mentioned by other answers. Instead of providing a second function taking an initializer_list, you may specify the type of the argument to foo when calling the function:
Deciding how to treat each parameter, is another question, however.
[EDIT]: Idea is taken from std::shared_ptr and initializer lists
This is bad. Consider a simple print() utility:
All of this would work:
Tuple also works (ignore the implementation required):
But none of this works
The above "solution" does not help too:
This (as above) does not give usable print() utility :
Is it something obvious that is missing here? I would like to mix anything in the call to print() as it's declaration implies.
Anybody?
[Edit 2017-11-08]
Somebody has suggested
And to somewhat remedy the pain of this, I am crushed to admit I have defined this macro "helper"
Usage:
But alas, MSVC 14.11.25503 (the latest as of time of this writing) can not compile this. With errors coming from
I am sure nobody want's the rest of the MSVC error dump ... Is it me or is it them?
Doing the print() as generic lambda does not solve anything of course.
Even if one passes single and simple init list this wont compile with the same error as above ...
I know C++17 type deduction of init lists is strengthened and improved, but I can not see in there anything to help me understand is this doable at all?
At last it seems it should be.
The problem is likely deducibility.
{}
could be uniform initializers to any of the arguments.This works:
See it Live on Coliru
The issue is not with the varadic arguments, but that the compiler cannot deduce the type of a brace enclosed initializer list, except for the case where you've declare the parameter of
std::initializer_list<T>
There's even an example right below