Running code i have gotten from google searches, ends up in errors. Pretty much what i got off a search is use c style or c++11 init lists. But both ways gives me an error, both as no matching function calls.
Is this the correct method? Are there other methods than just these?
ok, that was the reason for the first one. I'm not sure i yet understand the concept behind the non-recursive overload. It's an empty function named the same? Is it running that function for each argument?
the second one:
I am not sure if i need the brace or not:
1 2 3 4 5 6 7 8 9 10 11 12
test.cpp: In function ‘int main()’:
test.cpp:28:20: error: no matching function for call to ‘func2(int, double, char)’
func2(1,2.5,'a');
^
test.cpp:28:20: note: candidate is:
test.cpp:18:6: note: template<class T> void func2(std::initializer_list<_Tp>)
void func2( std::initializer_list<T> list )
^
test.cpp:18:6: note: template argument deduction/substitution failed:
test.cpp:28:20: note: mismatched types ‘std::initializer_list<_Tp>’ and ‘int’
func2(1,2.5,'a');
^
1 2 3 4 5 6 7 8 9 10 11 12
test.cpp: In function ‘int main()’:
test.cpp:29:18: error: no matching function for call to ‘func2(<brace-enclosed initializer list>)’
func2({1,2.5});
^
test.cpp:29:18: note: candidate is:
test.cpp:18:6: note: template<class T> void func2(std::initializer_list<_Tp>)
void func2( std::initializer_list<T> list )
^
test.cpp:18:6: note: template argument deduction/substitution failed:
test.cpp:29:18: note: deduced conflicting types for parameter ‘_Tp’ (‘int’ and ‘double’)
func2({1,2.5});
^
It's an empty function named the same? Is it running that function for each argument?
Your first function takes one or more arguments (one argument called t and zero-or-more arguments called args), and then calls itself with just the args.
When args holds nothing, it tries to call itself with no arguments, but you didn't define a func that could be called with no arguments, so the compilation was failing, likely with something like
test.cc:7:5: error: no matching function for call to 'func'
func(args...) ;
^~~~
test.cc:7:5: note: in instantiation of function template specialization 'func<char, >'
...
test.cc:4:6: note: candidate function template not viable: requires at least argument 't', but no arguments were provided
void func(T t, Args... args)
^
1 error generated.
Your second function of course needs a brace if you're trying to use initializer_list, but it is defined to take a list of objects all of the same type T, and when you try to pass many different types, there is no way to define such T that it would match them all at once, hence "deduced conflicting types for parameter ‘_Tp’ (‘int’ and ‘double’)"