variable function arguments

regarding varargs:

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <initializer_list>

template<typename T, typename... Args>
void func(T t, Args... args) 
{
    std::cout << t <<std::endl ;

    func(args...) ;
}

template <class T>
void func2( std::initializer_list<T> list )
{
    for( auto elem : list )
    {
        std::cout << elem << std::endl ;
    }
}

int main(){
    //func(1,2.5,'a');
    //func2(1,2.5,'a');
    //func2({1,2.5,'a'});

}


There also is the the method of sending a vector or some other container of arguments to a function, but assuming i did not want to do that method....
Last edited on
what is the error you're getting?

your first function is missing the non-recursive overload:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void func() {}

template<typename T, typename... Args>
void func(T t, Args... args)
{
    std::cout << t << '\n';
    func(args...) ;
}


int main(){
    func(1, 2.5, 'a');
    func(7, 2.5, 'a');
}
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});
                  ^

Last edited on
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’)"
on both cases, for some reason, such things did not occur to me.

Thanks for clearing it up.
Topic archived. No new replies allowed.