Pass initializer_list to variadic template

Jun 21, 2012 at 4:41pm
I'm trying to pass an initializer_list to a variadic template but it doesn't work.
1
2
3
4
5
6
7
8
9
10
11
12
#include <initializer_list>

template <class... Args>
void foo(std::initializer_list<double> values, Args... args)
{
}

int main()
{
	//error: too many arguments to function 'void foo(std::initializer_list<double, Args ...) [with Args = {}]>'
	foo({1, 2, 3}, {20, 30, 40});
}


If I change initializer_list to vector it generates the same kind of error so I guess it just don't like the {}-syntax. Is it not possible to do this?
Last edited on Jun 21, 2012 at 4:44pm
Jun 21, 2012 at 5:11pm
I think that then the compiler sees {20, 30, 40} it can not deduce the type of the parameter.
Last edited on Jun 21, 2012 at 5:14pm
Jun 21, 2012 at 5:15pm
The "{}-syntax" is not an expression, it cannot be used to deduce the type of a template parameter (with the special exceptions for auto and range-for loop).

Although GCC has (had?) a compiler extension where it deduces brace-enclosed lists as initializer_list, with a warning: "test.cc:11:29: warning: deducing ‘Args ...’ as ‘std::initializer_list<int>’ [enabled by default]"
Last edited on Jun 21, 2012 at 5:15pm
Topic archived. No new replies allowed.